服務(wù)器響應(yīng)
如需獲得來自服務(wù)器的響應(yīng),請(qǐng)使用 XMLHttpRequest 對(duì)象的 responseText 或 responseXML 屬性。
屬性 | 描述 |
---|
responseText | 獲得字符串形式的響應(yīng)數(shù)據(jù)。 |
responseXML | 獲得 XML 形式的響應(yīng)數(shù)據(jù)。 |
responseText 屬性
如果來自服務(wù)器的響應(yīng)并非 XML,請(qǐng)使用 responseText 屬性。
responseText 屬性返回字符串形式的響應(yīng),因此您可以這樣使用:
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
親自試一試
responseXML 屬性
如果來自服務(wù)器的響應(yīng)是 XML,而且需要作為 XML 對(duì)象進(jìn)行解析,請(qǐng)使用 responseXML 屬性:
請(qǐng)求 books.xml 文件,并解析響應(yīng):
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;
親自試一試