異步更新原理:用XMLHTTP發送請求獲得服務器端應答數據,在不從新載入整個頁面的狀況下,用js操做Dom最終更新頁面
1.建立XMLHttp請求協議node
1 function createXMLHttpRequest(){ 2 var xmlHttp; 3 if(window.ActiveXObject) { //IE瀏覽器 4 //IE瀏覽器(將XMLHttpRequest對象做爲ActiveX對象來建立) 5 try{ 6 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 7 }catch(e){ 8 try { 9 xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 10 }catch(e){} 11 } 12 }else{//非IE瀏覽器(將XMLHttpRequest對象做爲本地瀏覽器對象來建立) 13 xmlHttp = new XMLHttpRequest(); 14 } 15 if(xmlHttp == null){ 16 alert("不能建立XMLHttpRequest對象"); 17 return false; 18 } 19 return xmlHttp; 20 }
如需將請求發送到服務器,咱們使用 XMLHttpRequest 對象的 open() 和 send() 方法:ajax
xmlhttp.open("GET","test1.txt",true); xmlhttp.send();
一個簡單的 GET 請求:瀏覽器
xmlhttp.open("GET","demo_get.asp",true); xmlhttp.send();
在上面的例子中,您可能獲得的是緩存的結果。緩存
爲了不這種狀況,請向 URL 添加一個惟一的 ID:服務器
xmlhttp.open("GET","demo_get.asp?t=" + ,true); xmlhttp.send();Math.random()
一個簡單 POST 請求:app
xmlhttp.open("POST","demo_post.asp",true); xmlhttp.send();
親自試一試dom
若是須要像 HTML 表單那樣 POST 數據,請使用 setRequestHeader() 來添加 HTTP 頭。而後在 send() 方法中規定您但願發送的數據:異步
xmlhttp.open("POST","ajax_test.asp",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send("fname=Bill&lname=Gates");
當使用 async=true 時,請規定在響應處於 onreadystatechange 事件中的就緒狀態時執行的函數:async
//xmlhttp.readyState==4請求完成,0=未初始化;1=正在加載;2=已加載;3=交互中;4=完成函數
//xmlhttp.status==200請求狀態,200爲正常返回。
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","test1.txt",true); xmlhttp.send();
如需得到來自服務器的響應,請使用 XMLHttpRequest 對象的 responseText 或 responseXML 屬性。
屬性 | 描述 |
---|---|
responseText | 得到字符串形式的響應數據。 |
responseXML | 得到 XML 形式的響應數據。 |
若是來自服務器的響應並不是 XML,請使用 responseText 屬性。
responseText 屬性返回字符串形式的響應,所以您能夠這樣使用:
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
注意:xmlHttp.responseText返回時有多是一組數據【josn..】,咱們就須要將其轉換爲對象了。
var obj=eval("("+xmlHttp.responseText+")");
若是來自服務器的響應是 XML,並且須要做爲 XML 對象進行解析,請使用 responseXML 屬性:
請求 books.xml 文件,並解析響應:
xmlDoc=xmlhttp.responseXML; txt=""; x=xmlDoc.getElementsByTagName("ARTIST"); for (i=0;i<x.length;i++) { txt=txt + x[i].childNodes[0].nodeValue + "<br />"; } document.getElementById("myDiv").innerHTML=txt;
當請求被髮送到服務器時,咱們須要執行一些基於響應的任務。
每當 readyState 改變時,就會觸發 onreadystatechange 事件。
readyState 屬性存有 XMLHttpRequest 的狀態信息。
下面是 XMLHttpRequest 對象的三個重要的屬性:
屬性 | 描述 |
---|---|
onreadystatechange | 存儲函數(或函數名),每當 readyState 屬性改變時,就會調用該函數。 |
readyState | 存有 XMLHttpRequest 的狀態。從 0 到 4 發生變化。
|
status | 200: "OK" 404: 未找到頁面 |
在 onreadystatechange 事件中,咱們規定當服務器響應已作好被處理的準備時所執行的任務。
當 readyState 等於 4 且狀態爲 200 時,表示響應已就緒:
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }
註釋:onreadystatechange 事件被觸發 4 次,對應着 readyState 的每一個變化。
callback 函數是一種以參數形式傳遞給另外一個函數的函數。
若是您的網站上存在多個 AJAX 任務,那麼您應該爲建立 XMLHttpRequest 對象編寫一個標準的函數,併爲每一個 AJAX 任務調用該函數。
該函數調用應該包含 URL 以及發生 onreadystatechange 事件時執行的任務(每次調用可能不盡相同):
function myFunction() { loadXMLDoc("ajax_info.txt",function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }); }