XmlHttpRequest 对象详解
生活随笔
收集整理的這篇文章主要介紹了
XmlHttpRequest 对象详解
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最通用的定義為:XmlHttp是一套可以在Javascript、VbScript、Jscript等腳本語言中通過http協(xié)議傳送或從接收XML及其他數(shù)據(jù)的一套API。XmlHttp最大的用處是可以更新網(wǎng)頁的部分內(nèi)容而不需要刷新整個(gè)頁面。
來自MSDN的解釋:XmlHttp提供客戶端同http服務(wù)器通訊的協(xié)議。客戶端可以通過XmlHttp對象(MSXML2.XMLHTTP.3.0)向 http服務(wù)器發(fā)送請求并使用微軟XML文檔對象模型Microsoft? XML Document Object Model (DOM)處理回應(yīng)。 現(xiàn)在的絕對多數(shù)瀏覽器都增加了對XmlHttp的支持,IE中使用ActiveXObject方式創(chuàng)建XmlHttp對象,其他瀏覽器如:Firefox、Opera等通過window.XMLHttpRequest來創(chuàng)建xmlhttp對象。
* 表示此屬性是W3C文檔對象模型的擴(kuò)展.
-------------------------
--------------------------
--------------------------
詳細(xì)說明::
XMLHttpRequest 是 Ajax 的關(guān)鍵技術(shù),然而XMLHttpRequest 并非W3C標(biāo)準(zhǔn)。它目前所完成的大量功能將被過渡到W3C的新項(xiàng)目“DOM Level 3 Load and Save”標(biāo)準(zhǔn)里面。通過XMLHttpReques,web頁可以從web 服務(wù)器得到反饋和需求而不用重新加載頁面。用戶將停留在相同的頁面,而不會(huì)注意到腳本可能在后臺(tái)需求某頁面或是在給服務(wù)器發(fā)送數(shù)據(jù)。Google Suggest 就是用XMLHttpRequest對象來建立的一個(gè)動(dòng)態(tài)web 接口:當(dāng)你開始在Google 的搜索框中打字時(shí),一個(gè)JS腳本發(fā)送字母到一服務(wù)器并從服務(wù)器返回一列建議。W3C “DOM Level 3 Load and Save”標(biāo)準(zhǔn)包含一些相似的功能,但是這些還不能在任何一瀏覽器中得到實(shí)現(xiàn)。所以就目前,如你需要從瀏覽器發(fā)送HTTP請求,你還是得用到 XMLHttpRequest 對象。Javascript 也是靠XMLHttpRequest 來獲取XML的。對于不同的瀏覽器,創(chuàng)建 XMLHttpRequest 對象的方式有些不一樣,IE把XMLHttpRequest實(shí)現(xiàn)為一個(gè)ActiveX對象,其他瀏覽器把它實(shí)現(xiàn)為一個(gè)本地JavaScript對象。經(jīng)過綜合多方的腳本,采用以下腳本基本上能夠滿足在各種瀏覽器中創(chuàng)建XMLHttpRequest 的需求。 <script type=”text/javascript”>?
var?xmlHttp;?
function?creatXMLHttpRequest() {?
if?(window.ActiveXObject) {?
xmlHttp =?new?ActiveXObject(”Microsoft.XMLHTTP”);?
}?
else?if?(window.XMLHttpRequest) {?
xmlHttp =?new?XMLHttpRequest();?
}?
else?{?
return;?
}?
} 其中XMLHttpRequest對象包含了一些方法以及屬性,先不管它們,等用到了再看。 Methods(方法) abort()?
Cancels the current request 取消當(dāng)前的請求 getAllResponseHeaders()?
Returns the complete set of http headers as a string 以字符串的形式返回完整的HTTP頭信息 getResponseHeader(”headername”)?
Returns the value of the specified http header 返回指定的HTTP頭信息值 open(”method”,”URL”,async,”uname”,”pswd”)?
Specifies the method, URL, and other optional attributes of a request 為一請求指定發(fā)放,URL,和其他的任意屬性。 The method parameter can have a value of “GET”, “POST”, or “PUT” (use “GET” when requesting data and use “POST” when sending data (especially if the length of the data is greater than 512 bytes. 方法參數(shù)可以是 “GET”, “POST”, 或 “PUT” 中的一個(gè)(請求數(shù)據(jù)使用GET比較多而POST發(fā)送數(shù)據(jù)[特別是長度大于512字節(jié)的數(shù)據(jù)]) The URL parameter may be either a relative or complete URL. URL可以是絕對路徑或是相對的路徑。 The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing 異步參數(shù)指明是否應(yīng)該處理請求。設(shè)置成“True”的意思是在send()方法結(jié)束后腳本繼續(xù)執(zhí)行,而不需要等待服務(wù)器的回應(yīng)。“False”則是腳本必須等待服務(wù)器的回應(yīng)后才能繼續(xù)執(zhí)行。 send(content)?
Sends the request 發(fā)送請求 setRequestHeader(”label”,”value”)?
Adds a label/value pair to the http header to be sent Properties屬性 onreadystatechange* :
An event handler for an event that fires at every state change, typically a call to a JavaScript function.
這個(gè)是個(gè)最重要的屬性,為每次狀態(tài)的變化而準(zhǔn)備的事件處理,往往用于觸發(fā)一個(gè)JavaScript運(yùn)行。 readyState :
Returns the state of the object:
返回的狀態(tài)對象:
Returns the response as a string
以字符串形式返回 responseXML :?
Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
以XML的形式返回,這個(gè)屬性返回一XML文檔對象,可用W3C的DOM點(diǎn)樹方法和屬性來進(jìn)行解析和檢驗(yàn)。 status :?
Returns the status as a number (e.g. 404 for “Not Found” or 200 for “OK”)
以數(shù)字的形式返回狀態(tài)(比如404是”沒有找到“或200是”好的“) statusText:?
Returns the status as a string (e.g. “Not Found” or “OK”)
以字符串形式返回狀態(tài)(比如”沒有找到“或”好的“)
步驟一:”請求” — 如何發(fā)送一個(gè)HTTP請求? 發(fā)送HTTP請求是關(guān)鍵,我們先總結(jié)一下步驟,一共是4步: 1. 獲得一個(gè)XMLHttpRequest實(shí)例,可以通過創(chuàng)建,或者訪問已存在的XMLHttpRequest對象實(shí)例。 <script type=”text/javascript”>?
????????var?xmlHttp;?
????????function?creatXMLHttpRequest() {?
????????if?(window.ActiveXObject) {?
????????xmlHttp =?new?ActiveXObject(”Microsoft.XMLHTTP”);?
????????}?
????????else?if?(window.XMLHttpRequest) {?
????????xmlHttp =?new?XMLHttpRequest();?
????????}?
????????else?{?
????????return;?
????????}?
????????} ?2. 接下來要決定當(dāng)收到服務(wù)器的響應(yīng)后,需要做什么。這需要告訴HTTP請求對象用哪一個(gè)JavaScript函數(shù)處理這個(gè)響應(yīng)。可以將對象的onreadystatechange屬性設(shè)置為要使用的JavaScript的函數(shù)名。xmlHttp.onreadystatechange = handleStateChange; 注意:在函數(shù)名后沒有括號(hào),也無需傳遞參數(shù)。 3. 在定義了如何處理響應(yīng)后,就要發(fā)送請求了。可以調(diào)用HTTP請求類的open()和send()方法, 如: xmlHttp.open(”GET”, “simpleResponse.xml”, true);
??? xmlHttp.send(null); 關(guān)于open()后面的幾個(gè)參數(shù)這里要解釋一下了。第一個(gè)參數(shù)是HTTP請求方式 – GET,POST,HEAD 或任何服務(wù)器所支持的您想調(diào)用的方式。按照HTTP規(guī)范,該參數(shù)要大寫;否則,某些瀏覽器(如FireFox) 可能無法處理請求。第二個(gè)參數(shù)是請求頁面的 URL。由于自身安全特性的限制,該頁面不能為第三方域名的頁面。同時(shí)一定要保證在所有的頁面中都使用準(zhǔn)確的域名,否則調(diào)用 open()會(huì)得到“permission denied”的錯(cuò)誤提示。一個(gè)常見的錯(cuò)誤是訪問站點(diǎn)時(shí)使用domain.tld,而當(dāng)請求頁面時(shí),卻使用“www.domain.tld”。第三個(gè)參數(shù)設(shè)置請求是否為異步模式。如果是ture, JavaScript函數(shù)將繼續(xù)執(zhí)行,而不等待服務(wù)器響應(yīng)。這就是”AJAX”中的”Asynchronous”。 4. 向服務(wù)器發(fā)送請求。send()方法向指定的目標(biāo)資源發(fā)送請求。send()方法允許一個(gè)參數(shù),可以是一個(gè)字符串或者一個(gè)DOM對象。這個(gè)參數(shù)會(huì)作為請求本身的一部分被傳輸?shù)侥康腢RL。當(dāng)send()方法里面包含有參數(shù)的時(shí)候,要確定open()方法里面的第一個(gè)參數(shù)是“POST”。如果沒有數(shù)據(jù)要作為請求本身的一部分發(fā)送,就使用“null”,正如我們的例子中使用的。
步驟 2 – “收到” — 處理服務(wù)器的響應(yīng) 當(dāng)發(fā)送請求時(shí),要提供指定處理響應(yīng)的JavaScript函數(shù)名,步驟一的第二點(diǎn)我們已經(jīng)定義了這個(gè)函數(shù)(handleStateChange)。我們來看看這個(gè)函數(shù)的功能是什么。首先函數(shù)會(huì)檢查請求的狀態(tài),如果狀態(tài)值是4,就意味著一個(gè)完整的服務(wù)器響應(yīng)已經(jīng)收到了,您將可以處理該響應(yīng)。 if (XMLHttp.readyState == 4) {
??? // everything is good, the response is received
??? } else {
??? // still not ready
??? } readyState的取值如下:
// perfect!
} else {
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
} 在檢查完請求的狀態(tài)值和響應(yīng)的HTTP狀態(tài)值后, 您就可以處理從服務(wù)器得到的數(shù)據(jù)了。有兩種方式可以得到這些數(shù)據(jù): xmlHttp.responseText – 以文本字符串的方式返回服務(wù)器的響應(yīng)
xmlHttp.responseXML – 以XMLDocument對象方式返回響應(yīng)。處理XMLDocument對象可以用JavaScript DOM函數(shù) 整理一下步驟2的代碼,就是處理服務(wù)器相應(yīng)函數(shù)(handleStateChange): function?handleStateChange() {?
if(xmlHttp.readyState == 4) {?
if(xmlHttp.status == 200) {?
alert(”The server repilied?with: ” + xmlHttp.responseText);?
}?
}?
} 步驟 3 – “萬事俱備” - 簡單實(shí)例 我們現(xiàn)在將整個(gè)過程完整地做一次。 發(fā)送一個(gè)簡單的HTTP請求。我們用JavaScript請求一個(gè)XML文件,simpleResponse.xml,文件的文本內(nèi)容為”Hello from the server!”,然后我們”alert()”simpleResponse.xml文件的內(nèi)容。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<A?href="http://www.w3.org/TR/html4/strict.dtd">http://www.w3.org/TR/html4/strict.dtd</A>">?
<html>?
????<head>?
????????<meta?http-equiv="Content-Type"?content="text/html; charset=iso-8859-1">?
????????<title>Simple XMLHttpRequest</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();//創(chuàng)建XMLHttpRequest?
??????????????? xmlHttp.onreadystatechange = handleStateChange;?
??????????????? xmlHttp.open("GET", "simpleResponse.xml", true);?
??????????????? xmlHttp.send(null);?
??????????? }?
???????????????
??????????? function handleStateChange(){?
??????????????? if (xmlHttp.readyState == 4) {?
??????????????????? if (xmlHttp.status == 200) {?
??????????????????????? alert("The server replied with:" + xmlHttp.responseText);?
??????????????????? }?
??????????????? }?
??????????? }?
????????</script>?
????</head>?
????<body>?
????????<form?action="#">?
????????????<input?type="button"?value="Start Basic Asynchronous Request"?onclick="startRequest();"/>?
????????</form>?
????</body>?
</html> 本文轉(zhuǎn)自sucre03 51CTO博客,原文鏈接:http://blog.51cto.com/sucre/354165,如需轉(zhuǎn)載請自行聯(lián)系原作者
來自MSDN的解釋:XmlHttp提供客戶端同http服務(wù)器通訊的協(xié)議。客戶端可以通過XmlHttp對象(MSXML2.XMLHTTP.3.0)向 http服務(wù)器發(fā)送請求并使用微軟XML文檔對象模型Microsoft? XML Document Object Model (DOM)處理回應(yīng)。 現(xiàn)在的絕對多數(shù)瀏覽器都增加了對XmlHttp的支持,IE中使用ActiveXObject方式創(chuàng)建XmlHttp對象,其他瀏覽器如:Firefox、Opera等通過window.XMLHttpRequest來創(chuàng)建xmlhttp對象。
XmlHttp對象參考:
屬性:
| onreadystatechange* | 指定當(dāng)readyState屬性改變時(shí)的事件處理句柄。只寫 |
| readyState | 返回當(dāng)前請求的狀態(tài),只讀. |
| responseBody | 將回應(yīng)信息正文以u(píng)nsigned byte數(shù)組形式返回.只讀 |
| responseStream | 以Ado Stream對象的形式返回響應(yīng)信息。只讀 |
| responseText | 將響應(yīng)信息作為字符串返回.只讀 |
| responseXML | 將響應(yīng)信息格式化為Xml Document對象并返回,只讀 |
| status | 返回當(dāng)前請求的http狀態(tài)碼.只讀 |
| statusText | 返回當(dāng)前請求的響應(yīng)行狀態(tài),只讀 |
方法:
| abort | 取消當(dāng)前請求 |
| getAllResponseHeaders | 獲取響應(yīng)的所有http頭 |
| getResponseHeader | 從響應(yīng)信息中獲取指定的http頭 |
| open | 創(chuàng)建一個(gè)新的http請求,并指定此請求的方法、URL以及驗(yàn)證信息(用戶名/密碼) |
| send | 發(fā)送請求到http服務(wù)器并接收回應(yīng) |
| setRequestHeader | 單獨(dú)指定請求的某個(gè)http頭 |
事件:
無-------------------------
--------------------------
--------------------------
詳細(xì)說明::
XMLHttpRequest 是 Ajax 的關(guān)鍵技術(shù),然而XMLHttpRequest 并非W3C標(biāo)準(zhǔn)。它目前所完成的大量功能將被過渡到W3C的新項(xiàng)目“DOM Level 3 Load and Save”標(biāo)準(zhǔn)里面。通過XMLHttpReques,web頁可以從web 服務(wù)器得到反饋和需求而不用重新加載頁面。用戶將停留在相同的頁面,而不會(huì)注意到腳本可能在后臺(tái)需求某頁面或是在給服務(wù)器發(fā)送數(shù)據(jù)。Google Suggest 就是用XMLHttpRequest對象來建立的一個(gè)動(dòng)態(tài)web 接口:當(dāng)你開始在Google 的搜索框中打字時(shí),一個(gè)JS腳本發(fā)送字母到一服務(wù)器并從服務(wù)器返回一列建議。W3C “DOM Level 3 Load and Save”標(biāo)準(zhǔn)包含一些相似的功能,但是這些還不能在任何一瀏覽器中得到實(shí)現(xiàn)。所以就目前,如你需要從瀏覽器發(fā)送HTTP請求,你還是得用到 XMLHttpRequest 對象。Javascript 也是靠XMLHttpRequest 來獲取XML的。對于不同的瀏覽器,創(chuàng)建 XMLHttpRequest 對象的方式有些不一樣,IE把XMLHttpRequest實(shí)現(xiàn)為一個(gè)ActiveX對象,其他瀏覽器把它實(shí)現(xiàn)為一個(gè)本地JavaScript對象。經(jīng)過綜合多方的腳本,采用以下腳本基本上能夠滿足在各種瀏覽器中創(chuàng)建XMLHttpRequest 的需求。 <script type=”text/javascript”>?
var?xmlHttp;?
function?creatXMLHttpRequest() {?
if?(window.ActiveXObject) {?
xmlHttp =?new?ActiveXObject(”Microsoft.XMLHTTP”);?
}?
else?if?(window.XMLHttpRequest) {?
xmlHttp =?new?XMLHttpRequest();?
}?
else?{?
return;?
}?
} 其中XMLHttpRequest對象包含了一些方法以及屬性,先不管它們,等用到了再看。 Methods(方法) abort()?
Cancels the current request 取消當(dāng)前的請求 getAllResponseHeaders()?
Returns the complete set of http headers as a string 以字符串的形式返回完整的HTTP頭信息 getResponseHeader(”headername”)?
Returns the value of the specified http header 返回指定的HTTP頭信息值 open(”method”,”URL”,async,”uname”,”pswd”)?
Specifies the method, URL, and other optional attributes of a request 為一請求指定發(fā)放,URL,和其他的任意屬性。 The method parameter can have a value of “GET”, “POST”, or “PUT” (use “GET” when requesting data and use “POST” when sending data (especially if the length of the data is greater than 512 bytes. 方法參數(shù)可以是 “GET”, “POST”, 或 “PUT” 中的一個(gè)(請求數(shù)據(jù)使用GET比較多而POST發(fā)送數(shù)據(jù)[特別是長度大于512字節(jié)的數(shù)據(jù)]) The URL parameter may be either a relative or complete URL. URL可以是絕對路徑或是相對的路徑。 The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing 異步參數(shù)指明是否應(yīng)該處理請求。設(shè)置成“True”的意思是在send()方法結(jié)束后腳本繼續(xù)執(zhí)行,而不需要等待服務(wù)器的回應(yīng)。“False”則是腳本必須等待服務(wù)器的回應(yīng)后才能繼續(xù)執(zhí)行。 send(content)?
Sends the request 發(fā)送請求 setRequestHeader(”label”,”value”)?
Adds a label/value pair to the http header to be sent Properties屬性 onreadystatechange* :
An event handler for an event that fires at every state change, typically a call to a JavaScript function.
這個(gè)是個(gè)最重要的屬性,為每次狀態(tài)的變化而準(zhǔn)備的事件處理,往往用于觸發(fā)一個(gè)JavaScript運(yùn)行。 readyState :
Returns the state of the object:
返回的狀態(tài)對象:
- 0 = uninitialized[初始化]
- 1 = loading[加載中]
- 2 = loaded[加載完畢]
- 3 = interactive[交互]
- 4 = complete [完畢]
Returns the response as a string
以字符串形式返回 responseXML :?
Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties
以XML的形式返回,這個(gè)屬性返回一XML文檔對象,可用W3C的DOM點(diǎn)樹方法和屬性來進(jìn)行解析和檢驗(yàn)。 status :?
Returns the status as a number (e.g. 404 for “Not Found” or 200 for “OK”)
以數(shù)字的形式返回狀態(tài)(比如404是”沒有找到“或200是”好的“) statusText:?
Returns the status as a string (e.g. “Not Found” or “OK”)
以字符串形式返回狀態(tài)(比如”沒有找到“或”好的“)
步驟一:”請求” — 如何發(fā)送一個(gè)HTTP請求? 發(fā)送HTTP請求是關(guān)鍵,我們先總結(jié)一下步驟,一共是4步: 1. 獲得一個(gè)XMLHttpRequest實(shí)例,可以通過創(chuàng)建,或者訪問已存在的XMLHttpRequest對象實(shí)例。 <script type=”text/javascript”>?
????????var?xmlHttp;?
????????function?creatXMLHttpRequest() {?
????????if?(window.ActiveXObject) {?
????????xmlHttp =?new?ActiveXObject(”Microsoft.XMLHTTP”);?
????????}?
????????else?if?(window.XMLHttpRequest) {?
????????xmlHttp =?new?XMLHttpRequest();?
????????}?
????????else?{?
????????return;?
????????}?
????????} ?2. 接下來要決定當(dāng)收到服務(wù)器的響應(yīng)后,需要做什么。這需要告訴HTTP請求對象用哪一個(gè)JavaScript函數(shù)處理這個(gè)響應(yīng)。可以將對象的onreadystatechange屬性設(shè)置為要使用的JavaScript的函數(shù)名。xmlHttp.onreadystatechange = handleStateChange; 注意:在函數(shù)名后沒有括號(hào),也無需傳遞參數(shù)。 3. 在定義了如何處理響應(yīng)后,就要發(fā)送請求了。可以調(diào)用HTTP請求類的open()和send()方法, 如: xmlHttp.open(”GET”, “simpleResponse.xml”, true);
??? xmlHttp.send(null); 關(guān)于open()后面的幾個(gè)參數(shù)這里要解釋一下了。第一個(gè)參數(shù)是HTTP請求方式 – GET,POST,HEAD 或任何服務(wù)器所支持的您想調(diào)用的方式。按照HTTP規(guī)范,該參數(shù)要大寫;否則,某些瀏覽器(如FireFox) 可能無法處理請求。第二個(gè)參數(shù)是請求頁面的 URL。由于自身安全特性的限制,該頁面不能為第三方域名的頁面。同時(shí)一定要保證在所有的頁面中都使用準(zhǔn)確的域名,否則調(diào)用 open()會(huì)得到“permission denied”的錯(cuò)誤提示。一個(gè)常見的錯(cuò)誤是訪問站點(diǎn)時(shí)使用domain.tld,而當(dāng)請求頁面時(shí),卻使用“www.domain.tld”。第三個(gè)參數(shù)設(shè)置請求是否為異步模式。如果是ture, JavaScript函數(shù)將繼續(xù)執(zhí)行,而不等待服務(wù)器響應(yīng)。這就是”AJAX”中的”Asynchronous”。 4. 向服務(wù)器發(fā)送請求。send()方法向指定的目標(biāo)資源發(fā)送請求。send()方法允許一個(gè)參數(shù),可以是一個(gè)字符串或者一個(gè)DOM對象。這個(gè)參數(shù)會(huì)作為請求本身的一部分被傳輸?shù)侥康腢RL。當(dāng)send()方法里面包含有參數(shù)的時(shí)候,要確定open()方法里面的第一個(gè)參數(shù)是“POST”。如果沒有數(shù)據(jù)要作為請求本身的一部分發(fā)送,就使用“null”,正如我們的例子中使用的。
步驟 2 – “收到” — 處理服務(wù)器的響應(yīng) 當(dāng)發(fā)送請求時(shí),要提供指定處理響應(yīng)的JavaScript函數(shù)名,步驟一的第二點(diǎn)我們已經(jīng)定義了這個(gè)函數(shù)(handleStateChange)。我們來看看這個(gè)函數(shù)的功能是什么。首先函數(shù)會(huì)檢查請求的狀態(tài),如果狀態(tài)值是4,就意味著一個(gè)完整的服務(wù)器響應(yīng)已經(jīng)收到了,您將可以處理該響應(yīng)。 if (XMLHttp.readyState == 4) {
??? // everything is good, the response is received
??? } else {
??? // still not ready
??? } readyState的取值如下:
- 0 (未初始化)
- 1 (正在裝載)
- 2 (裝載完畢)
- 3 (交互中)
- 4 (完成)
// perfect!
} else {
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
} 在檢查完請求的狀態(tài)值和響應(yīng)的HTTP狀態(tài)值后, 您就可以處理從服務(wù)器得到的數(shù)據(jù)了。有兩種方式可以得到這些數(shù)據(jù): xmlHttp.responseText – 以文本字符串的方式返回服務(wù)器的響應(yīng)
xmlHttp.responseXML – 以XMLDocument對象方式返回響應(yīng)。處理XMLDocument對象可以用JavaScript DOM函數(shù) 整理一下步驟2的代碼,就是處理服務(wù)器相應(yīng)函數(shù)(handleStateChange): function?handleStateChange() {?
if(xmlHttp.readyState == 4) {?
if(xmlHttp.status == 200) {?
alert(”The server repilied?with: ” + xmlHttp.responseText);?
}?
}?
} 步驟 3 – “萬事俱備” - 簡單實(shí)例 我們現(xiàn)在將整個(gè)過程完整地做一次。 發(fā)送一個(gè)簡單的HTTP請求。我們用JavaScript請求一個(gè)XML文件,simpleResponse.xml,文件的文本內(nèi)容為”Hello from the server!”,然后我們”alert()”simpleResponse.xml文件的內(nèi)容。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<A?href="http://www.w3.org/TR/html4/strict.dtd">http://www.w3.org/TR/html4/strict.dtd</A>">?
<html>?
????<head>?
????????<meta?http-equiv="Content-Type"?content="text/html; charset=iso-8859-1">?
????????<title>Simple XMLHttpRequest</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();//創(chuàng)建XMLHttpRequest?
??????????????? xmlHttp.onreadystatechange = handleStateChange;?
??????????????? xmlHttp.open("GET", "simpleResponse.xml", true);?
??????????????? xmlHttp.send(null);?
??????????? }?
???????????????
??????????? function handleStateChange(){?
??????????????? if (xmlHttp.readyState == 4) {?
??????????????????? if (xmlHttp.status == 200) {?
??????????????????????? alert("The server replied with:" + xmlHttp.responseText);?
??????????????????? }?
??????????????? }?
??????????? }?
????????</script>?
????</head>?
????<body>?
????????<form?action="#">?
????????????<input?type="button"?value="Start Basic Asynchronous Request"?onclick="startRequest();"/>?
????????</form>?
????</body>?
</html> 本文轉(zhuǎn)自sucre03 51CTO博客,原文鏈接:http://blog.51cto.com/sucre/354165,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的XmlHttpRequest 对象详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信 登录 Scope 参数错误或没有
- 下一篇: Unity3D - UGUI的手动搭建