AJAX XMLHttpRequest 伺服器響應

伺服器響應

如需獲得來自伺服器的響應,請使用 XMLHttpRequest 物件的 responseText 或 responseXML 屬性。

屬性 描述
responseText 獲得字串形式的響應資料。
responseXML 獲得 XML 形式的響應資料。


responseText 屬性

如果來自伺服器的響應並非 XML,請使用 responseText 屬性。

responseText 屬性返回字串形式的響應,因此您可以這樣使用:

例項

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

嘗試一下 ?


responseXML 屬性

如果來自伺服器的響應是 XML,而且需要作為 XML 物件進行解析,請使用 responseXML 屬性:

例項

請求 cd_catalog.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;

嘗試一下 ?