ð ããžã«ã«ã»ã³ãŒãâã¡ããã®äžæè°ãªAJAXéæ³è¬åº§
ãããããã¿ããªïŒ ããžã«ã«ã»ã³ãŒãâã¡ããã ãïŒä»æ¥ã¯ãã£ããã¹ãããªéæ³ããAJAXïŒãšã€ãžã£ãã¯ã¹ïŒã®ããããããããéæ³ãã®ã話ããã¡ãããïŒãã®éæ³ã䜿ããšããŠã§ãããŒãžããªããŒãããªããŠãããã£ããããŒã¿ãæã£ãŠããã¡ãããã ïŒãŸãã§éæ³ã¿ããã§ããâš ãã£ããã«å匷ãããïŒ
ð AJAXã£ãŠãªãã«ïŒéæ³ã®ã²ã¿ã€ãæ¢æ€ã ïŒ
AJAXã£ãŠããã®ã¯ãããAsynchronous JavaScript and XMLãã£ãŠããé£ããèšèã®ç¥ãªãã ããµããïŒé£ãããïŒå€§äžå€«ãç°¡åã«èšããšãããããŒãžããªããŒãããªããŠãããã£ããæ
å ±ãæ°ããããéæ³ãã£ãŠããšã ãïŒ
ããšãã°ããéæ³å°å¥³ãã¡ã®ãããã£ãŒã«ãèŠãããŒãžããããšããã§ãããæ®éãªããå¥ã®éæ³å°å¥³ã®æ
å ±ãèŠãããšãã«ãããŒãžå
šéšãèªã¿èŸŒã¿çŽããªããšãããªãã®ãã§ãããAJAXã®éæ³ã䜿ãã°ãããŒãžã¯ãã®ãŸãŸã§ãæ°ããæ
å ±ã ãããµãã£ãšæŽæ°ã§ãã¡ãããã ãïŒãããã§ããïŒ
ð AJAXã®éæ³ããã£ãŠã¿ããïŒ
ããããå®éã«AJAXã®éæ³ããã£ãŠã¿ãããïŒãŸãã¯ããµãããªåªæ...ãããªãã£ããJavaScriptã®ã³ãŒããæžããŠãããïŒ
function getMagicalGirlInfo(girlName) {
// AJAXéæ³ã®æºå
var xhr = new XMLHttpRequest();
// éæ³ã®åªæïŒãµãŒããŒã«ãé¡ããããïŒ
xhr.open('GET', '<https://api.magical-world.com/girls/>' + girlName, true);
// éæ³ãçµãã£ããšãã®ãçŽæ
xhr.onload = function() {
if (xhr.status === 200) {
// éæ³ãæåããããçµæãèŠããã
var result = JSON.parse(xhr.responseText);
displayMagicalGirlInfo(result);
} else {
// éæ³ã倱æãã¡ãã£ãã...
console.log('ãããããéæ³å€±æãã¡ãã£ããïŒ');
}
};
// éæ³ãã¹ã¿ãŒãïŒ
xhr.send();
}
// éæ³å°å¥³ã®æ
å ±ãèŠããé¢æ°
function displayMagicalGirlInfo(girlInfo) {
document.getElementById('magical-girl-name').textContent = girlInfo.name;
document.getElementById('magical-girl-power').textContent = girlInfo.power;
document.getElementById('magical-girl-catchphrase').textContent = girlInfo.catchphrase;
}
// éæ³ã䜿ã£ãŠã¿ãã
getMagicalGirlInfo('ã«ãã£');
ããããããŠããªéæ³ãã§ãããïŒð
ð AJAXã®éæ³ã®ã¹ããã
éæ³ã®æºå: XMLHttpRequest ã£ãŠããéæ³ã®éå ·ãçšæããã®ã
éæ³ã®åªæ: open ã£ãŠããåªæã§ãã©ããªæ å ±ã欲ããã®ããã©ãã«èãã®ãã決ãããã ã
éæ³ãçµãã£ããšãã®çŽæ: onload ã§ãéæ³ãæåãããšãã«ã©ãããã決ããŠããã®ã
éæ³ãã¹ã¿ãŒãïŒ: send ã£ãŠããåªæã§ãå®éã«ãµãŒããŒã«èããŠã¿ããã ã
çµæãèŠãã: éæ³ãæåããããåŸãããæ å ±ãããŒãžã«è¡šç€ºããŠãã¿ããªã«èŠãã¡ãããïŒ
âš AJAXã®éæ³ã䜿ããšã©ããªããããšãããã®ïŒ
ããŒãžããã«ãã«æ»ãã: ãªããŒãããªããŠãããããèŠãŠã人ãã³ã£ãããããããã¹ã ãŒãºïŒ
éæ³ã®åã®ç¯çŽ: å¿ èŠãªæ å ±ã ããæã£ãŠãããããéæ³ã®åïŒéä¿¡éïŒã®ç¡é§é£ããããªãã®ã
ãªã¢ã«ã¿ã€ã ã¿ãã: ã©ãã©ãæ å ±ãæ°ããã§ããããããŸãã§éæ³ã¿ããã«ææ°ã®ãç¥ãããã²ããïŒ
ð æ°ãã€ããããšã¯ïŒéæ³äœ¿ãã®ãçŽæ
éæ³ã®å¯äœçšã«æ°ãã€ããŠ: AJAXã¯æ»ããã¿ã³ãæ··ä¹±ããã¡ããããšãããã®ãããããæ°ãã€ããŠãïŒ
éæ³ãå¹ããªãããšãããã: å€ãéæ³ã®é¡ïŒãã©ãŠã¶ïŒã§ã¯ããã®éæ³ã䜿ããªããããããªãã®ã
éæ³ã®äœ¿ãããã«æ³šæ: ãµãŒããŒããã«åªããããããããªã¯ãšã¹ããéããããªãããã«ããããã
ð ãŸãšã
AJAXã®éæ³ã䜿ãã°ããŠã§ãããŒãžããã£ãšãã£ãšãã©ãã©âšãã«ãã«ã«ãªã£ã¡ãããïŒèŠãŠã人ãã¡ã«ããŸãã§éæ³ããããã¿ãããªã¹ã ãŒãºãªäœéšããã¬ãŒã³ãã§ãããã ã
ãããã¿ããªãäžç·ã«AJAXã®éæ³ã®ç·Žç¿ãããŠãããŠããªãŠã§ãããŒãžãäœãããïŒéæ³ã®åãä¿¡ããŠãã³ãŒãã£ã³ã°ã楜ããã§ãïŒ
ã¿ããªã®å€¢ãšåžæãããªããŸãããã«ïŒããžã«ã«ã»ã³ãŒãâã¡ããã§ããïŒãŸãããïŒðâš
ãã®èšäºãæ°ã«å ¥ã£ãããµããŒããããŠã¿ãŸãããïŒ