AJAX - 服務(wù)器 響應(yīng)
如需獲得來自服務(wù)器的響應(yīng),請使用 XMLHttpRequest 對象的 responseText 或 responseXML 屬性。
ResponseText和ResponseXML?
響應(yīng)信息的內(nèi)容可能有多種不同的形式,例 如XML、純文本、(X)HTML或JavaScript對象符號(JSON)。我們可以根據(jù)所接收到的數(shù)據(jù)格式的不同,用兩種不同的方法來處理:使用 responseText或者responseXML。
responseText方法用于那些并非基于XML的格式。它把響應(yīng)信息作為字符串,返回精確的 內(nèi)容。純文本、(X)HTML和JSON都使用responseText。responseText不僅可以給頁面添加內(nèi)容,它在調(diào)試AJAX請求的時候也有用處。例如,你可能還沒有準(zhǔn)備好分析數(shù)據(jù),因為你還不知道所有的標(biāo)簽 是什么樣的,是XML格式的還是JSON文件。這就要求有一種用于檢測被分析數(shù)據(jù)的途徑。一旦你知道了所有的標(biāo)簽名稱,所需要做的事情就只是編寫代碼 了。 ?
responseXML的使用也相當(dāng)簡單。但是與JSON格式類似,XML要求進(jìn)行數(shù)據(jù)分析。我們需要執(zhí)行的第一項事務(wù)是識別出XML響應(yīng)信息中的根節(jié)點(diǎn)。
var?response?=?ajax.request.responseXML.documentElement;
下一步,我們通過名稱獲取所有的元素并得到它們的值:?
var?header?=?response.getElementsByTagName(’header’)[0].firstChild.data;?
var?description?=?response.getElementsByTagName(’description’)[0].firstChild.data;?
var?sourceUrl?=?response.getElementsByTagName(’sourceUrl’)[0].firstChild.data;
最后,我們把響應(yīng)信息顯示在相應(yīng)的div標(biāo)記中:
$("body").html("<b>"?+?header?+?"</b><br/>"
???????????????? +?description?+?"<br/><br/>"?
???????????????? +?"<a?href=’"?+?sourceUrl?+?"'>Download?the?source?files</a>";
responseText 屬性返回字符串形式的響應(yīng):
<!DOCTYPE html> <html> <head> <script> function loadXMLDoc() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","ajax_info.txt",true); xmlhttp.send(); } </script> </head> <body> <div id="myDiv"><h2>使用 AJAX 修改該文本內(nèi)容</h2></div> <button type="button" onclick="loadXMLDoc()">修改內(nèi)容</button> </body> </html>
如果來自服務(wù)器的響應(yīng)是 XML,而且需要作為 XML 對象進(jìn)行解析,請使用 responseXML 屬性
請求?cd_catalog.xml?文件,并解析響應(yīng):
<!DOCTYPE html> <html> <head> <script> function loadXMLDoc() { var xmlhttp; var txt,x,i; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { 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; } } xmlhttp.open("GET","cd_catalog.xml",true); xmlhttp.send(); } </script> </head> <body> <h2>My CD Collection:</h2> <div id="myDiv"></div> <button type="button" onclick="loadXMLDoc()">Get my CD collection</button> </body> </html>
實例:
<!DOCTYPE HTML> <html> <head> <meta charset=utf-8" /> <title>Using responseText with innerHTML</title> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest(){ if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); } } function startRequest(){ createXMLHttpRequest(); xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET","innerHTML.xml",true); xmlHttp.send(null); } function handleStateChange(){ if(xmlHttp.readyState == 4){ if(xmlHttp.status == 200){ document.getElementById("results").innerHTML=xmlHttp.responseText; } } } </script> </head> <body> <form> <input type="button" value="Search for Today's Activities" onclick="startRequest();"/> </form> <div id="results"></div> </body> </html>
innerHTML.xml文件
<?xml version="1.0" encoding="utf-8"?> <table border="1"> <tbody> <tr><th>Activity Name</th><th>Location</th><th>Time</th></tr> <tr><td>Waterskiing</td><td>Dock #1</td><td>9:00 AM</td></tr> <tr><td>Volleyball</td><td>East Court</td><td>2:00 PM</td></tr> <tr><td>Hiking</td><td>Trail 3</td><td>3:30 PM</td></tr> </tbody> </table>