AJAX - ·þÎñÆ÷ÏìÓ¦
onreadystatechange ÊôÐÔ
readyState ÊôÐÔ´æÁô XMLHttpRequest µÄ״̬¡£
onreadystatechange ÊôÐÔ¶¨Òåµ± readyState ·¢Éú±ä»¯Ê±Ö´Ðеĺ¯Êý¡£
status ÊôÐÔºÍ statusText ÊôÐÔ´æÓÐ XMLHttpRequest ¶ÔÏóµÄ״̬¡£
ÊôÐÔ | ÃèÊö |
---|---|
onreadystatechange | ¶¨ÒåÁ˵± readyState ÊôÐÔ·¢Éú¸Ä±äʱËùµ÷Óõĺ¯Êý¡£ |
readyState |
±£´æÁË XMLHttpRequest µÄ״̬¡£
|
status |
ÈçÐèÍêÕûÁÐ±í£¬Çë·ÃÎÊ Http ÏûÏ¢²Î¿¼ÊÖ²á |
statusText | ·µ»Ø×´Ì¬Îı¾£¨ÀýÈç "OK" »ò "Not Found"£© |
ÿµ± readyState ·¢Éú±ä»¯Ê±¾Í»áµ÷Óà onreadystatechange º¯Êý¡£
µ± readyState Ϊ 4£¬status Ϊ 200 ʱ£¬ÏìÓ¦¾ÍÐ÷£º
ʵÀý
function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange
= function() { if (this.readyState
== 4 && this.status
== 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); }
×¢ÊÍ£ºonreadystatechange ±»´¥·¢Îå´Î£¨0-4£©£¬Ã¿´Î readyState ¶¼·¢Éú±ä»¯¡£
ʹÓûص÷º¯Êý
»Øµ÷º¯ÊýÊÇÒ»ÖÖ×÷Ϊ²ÎÊý±»´«µÝµ½ÁíÒ»¸öº¯ÊýµÄº¯Êý¡£
Èç¹ûÄúµÄÍøÕ¾ÖÐÓжà¸ö AJAX ÈÎÎñ£¬ÄÇôÄúÓ¦¸Ã´´½¨Ò»¸öÖ´ÐÐ XMLHttpRequest ¶ÔÏóµÄº¯Êý£¬ÒÔ¼°Ò»¸ö¹©Ã¿¸ö AJAX ÈÎÎñµÄ»Øµ÷º¯Êý¡£
¸Ãº¯ÊýÓ¦µ±°üº¬ URL ÒÔ¼°µ±ÏìÓ¦¾ÍÐ÷ʱµ÷Óõĺ¯Êý¡£
ʵÀý
loadDoc("url-1", myFunction1); loadDoc("url-2", myFunction2); function loadDoc(url, cFunction) { var xhttp; xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { cFunction(this); } }; xhttp.open("GET", url, true); xhttp.send(); } function myFunction1(xhttp) { // action goes here } function myFunction2(xhttp) { // action goes here }
·þÎñÆ÷ÏìÓ¦ÊôÐÔ
ÊôÐÔ | ÃèÊö |
---|---|
responseText | »ñÈ¡×Ö·û´®ÐÎʽµÄÏìÓ¦Êý¾Ý |
responseXML | »ñÈ¡ XML Êý¾ÝÐÎʽµÄÏìÓ¦Êý¾Ý |
·þÎñÆ÷ÏìÓ¦·½·¨
·½·¨ | ÃèÊö |
---|---|
getResponseHeader() | ´Ó·þÎñÆ÷·µ»ØÌض¨µÄÍ·²¿ÐÅÏ¢ |
getAllResponseHeaders() | ´Ó·þÎñÆ÷·µ»ØËùÓÐÍ·²¿ÐÅÏ¢ |
responseText ÊôÐÔ
responseText ÊôÐÔÒÔ JavaScript ×Ö·û´®µÄÐÎʽ·µ»Ø·þÎñÆ÷ÏìÓ¦£¬Òò´ËÄú¿ÉÒÔÕâÑùʹÓÃËü£º
ʵÀý
document.getElementById("demo").innerHTML = xhttp.responseText;
responseXML ÊôÐÔ
XML HttpRequest ¶ÔÏóÓÐÒ»¸öƒÈ½¨µÄ XML ½âÎöÆ÷¡£
ResponseXML ÊôÐÔÒÔ XML DOM ¶ÔÏ󷵻طþÎñÆ÷ÏìÓ¦¡£
ʹÓôËÊôÐÔ£¬Äú¿ÉÒÔ°ÑÏìÓ¦½âÎöΪ XML DOM ¶ÔÏó£º
ʵÀý
ÇëÇóÎļþ music_list.xml£¬²¢¶ÔÏìÓ¦½øÐнâÎö£º
xmlDoc = xhttp.responseXML
;
txt = "";
x = xmlDoc.getElementsByTagName("ARTIST");
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
xhttp.open("GET", "music_list.xml", true);
xhttp.send();
Äú½«ÔÚ±¾½Ì³ÌµÄ DOM Õ½Úѧµ½¸ü¶àÓÐ¹Ø XML DOM µÄ֪ʶ¡£
getAllResponseHeaders() ·½·¨
getAllResponseHeaders() ·½·¨·µ»ØËùÓÐÀ´×Ô·þÎñÆ÷ÏìÓ¦µÄÍ·²¿ÐÅÏ¢¡£
ʵÀý
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.getAllResponseHeaders()
;
}
};
getResponseHeader() ·½·¨
getResponseHeader() ·½·¨·µ»ØÀ´×Ô·þÎñÆ÷ÏìÓ¦µÄÌØ¶¨Í·²¿ÐÅÏ¢¡£
ʵÀý
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.getResponseHeader("Last-Modified")
;
}
};
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();