PHPプログラミング実戦15-5

1091 ワード








Original content

var xhr = new XMLHttpRequest(); // URL xhr.open("GET", window.location.pathname, true); // window.location.pathname==untitled/a.php // xhr.onreadystatechange = function () { //console.log(xhr.readyState); if (xhr.readyState == 4) { var message = ""; //console.log(xhr.status); if (xhr.status = 200) { message = "Ajax loaded content"; } else { message = "An error has occured making the request"; } document.getElementsByTagName("p")[0].innerHTML = message; } } // xhr.send(null);
  • open()で使用するURLは、現在のページwindowである.location.pathnameは、その初期値「Original content」から「Ajax loaded content」への変更
  • を見ることができる.
  • xhr.send(null)送信要求はデータ
  • を含まない
  • DOMツリー全体をロードする必要があるため、JavaScriptはHTML要素の後に置かれる.