XMLHttpRequest
提供客戶(hù)端同http服務(wù)器通訊的協(xié)議
Dim HttpReq As New MSXML2.XMLHTTP30 HttpReq.open "GET", "http://localhost/books.xml", False HttpReq.send MsgBox HttpReq.responseText
備注
客戶(hù)端可以通過(guò)XmlHttp對象(MSXML2.XMLHTTP.3.0)向http服務(wù)器發(fā)送請求并使用微軟XML文檔對象模型Microsoft® XML Document Object Model (DOM)處理回應。
XMLHttpRequest成員
屬性
* 表示此屬性是W3C文檔對象模型的擴展.
方法
onreadystatechange
指定當readyState屬性改變時(shí)的事件處理句柄
語(yǔ)法
oXMLHttpRequest.onreadystatechange = funcMyHandler;
如下的例子演示當XMLHTTPRequest對象的readyState屬性改變時(shí)調用HandleStateChange函數,當數據接收完畢后(readystate == 4)此頁(yè)面上的一個(gè)按鈕將被激活
var xmlhttp=null; function PostOrder(xmldoc) { var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0"); xmlhttp.Open("POST", "http://myserver/orders/processorder.asp", false); xmlhttp.onreadystatechange= HandleStateChange; xmlhttp.Send(xmldoc); myButton.disabled = true; } function HandleStateChange() { if (xmlhttp.readyState == 4) { myButton.disabled = false; alert("Result = " + xmlhttp.responseXML.xml); } }
備注
此屬性只寫(xiě),為W3C文檔對象模型的擴展.
readyState
返回XMLHTTP請求的當前狀態(tài)
語(yǔ)法
lValue = oXMLHttpRequest.readyState;
var XmlHttp; XmlHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); function send() { XmlHttp.onreadystatechange = doHttpReadyStateChange; XmlHttp.open("GET", "http://localhost/sample.xml", true); XmlHttp.send(); } function doHttpReadyStateChange() { if (XmlHttp.readyState == 4) { alert("Done"); } }
備注
變量,此屬性只讀,狀態(tài)用長(cháng)度為4的整型表示.定義如下:
| 0 (未初始化) | 對象已建立,但是尚未初始化(尚未調用open方法) |
| 1 (初始化) | 對象已建立,尚未調用send方法 |
| 2 (發(fā)送數據) | send方法已調用,但是當前的狀態(tài)及http頭未知 |
| 3 (數據傳送中) | 已接收部分數據,因為響應及http頭不全,這時(shí)通過(guò)responseBody和responseText獲取部分數據會(huì )出現錯誤, |
| 4 (完成) | 數據接收完畢,此時(shí)可以通過(guò)通過(guò)responseBody和responseText獲取完整的回應數據 |
responseBody
返回某一格式的服務(wù)器響應數據
語(yǔ)法
strValue = oXMLHttpRequest.responseBody;
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.responseBody);
備注
變量,此屬性只讀,以unsigned array格式表示直接從服務(wù)器返回的未經(jīng)解碼的二進(jìn)制數據。
responseStream
以Ado Stream對象的形式返回響應信息
語(yǔ)法
strValue = oXMLHttpRequest.responseStream;
備注
變量,此屬性只讀,以Ado Stream對象的形式返回響應信息。
responseText
將響應信息作為字符串返回
語(yǔ)法
strValue = oXMLHttpRequest.responseText;
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.responseText);
備注
變量,此屬性只讀,將響應信息作為字符串返回。
XMLHTTP嘗試將響應信息解碼為Unicode字符串,XMLHTTP默認將響應數據的編碼定為UTF-8,如果服務(wù)器返回的數據帶BOM(byte-order mark),XMLHTTP可以解碼任何UCS-2 (big or little endian)或者UCS-4 數據。注意,如果服務(wù)器返回的是xml文檔,此屬性并不處理xml文檔中的編碼聲明。你需要使用responseXML來(lái)處理。
responseXML
將響應信息格式化為Xml Document對象并返回
語(yǔ)法
var objDispatch = oXMLHttpRequest.responseXML;
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.responseXML.xml);
備注
變量,此屬性只讀,將響應信息格式化為Xml Document對象并返回。如果響應數據不是有效的XML文檔,此屬性本身不返回XMLDOMParseError,可以通過(guò)處理過(guò)的DOMDocument對象獲取錯誤信息。
status
返回當前請求的http狀態(tài)碼
語(yǔ)法
lValue = oXMLHttpRequest.status;
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.status);
返回值
長(cháng)整形標準http狀態(tài)碼,定義如下:
| 100 | Continue |
| 101 | Switching protocols |
| 200 | OK |
| 201 | Created |
| 202 | Accepted |
| 203 | Non-Authoritative Information |
| 204 | No Content |
| 205 | Reset Content |
| 206 | Partial Content |
| 300 | Multiple Choices |
| 301 | Moved Permanently |
| 302 | Found |
| 303 | See Other |
| 304 | Not Modified |
| 305 | Use Proxy |
| 307 | Temporary Redirect |
| 400 | Bad Request |
| 401 | Unauthorized |
| 402 | Payment Required |
| 403 | Forbidden |
| 404 | Not Found |
| 405 | Method Not Allowed |
| 406 | Not Acceptable |
| 407 | Proxy Authentication Required |
| 408 | Request Timeout |
| 409 | Conflict |
| 410 | Gone |
| 411 | Length Required |
| 412 | Precondition Failed |
| 413 | Request Entity Too Large |
| 414 | Request-URI Too Long |
| 415 | Unsupported Media Type |
| 416 | Requested Range Not Suitable |
| 417 | Expectation Failed |
| 500 | Internal Server Error |
| 501 | Not Implemented |
| 502 | Bad Gateway |
| 503 | Service Unavailable |
| 504 | Gateway Timeout |
| 505 | HTTP Version Not Supported |
備注
長(cháng)整形,此屬性只讀,返回當前請求的http狀態(tài)碼,此屬性?xún)H當數據發(fā)