XMLHttpRequest
提供客戶端同http服務器通訊的協議
Dim HttpReq As New MSXML2.XMLHTTP30 HttpReq.open "GET", "http://localhost/books.xml", False HttpReq.send MsgBox HttpReq.responseText
備註
客戶端可以通過XmlHttp對象(MSXML2.XMLHTTP.3.0)向http服務器發送請求並使用微軟XML文檔對象模型Microsoft® XML Document Object Model (DOM)處理迴應。
XMLHttpRequest成員
屬性
onreadystatechange | 指定當readyState屬性改變時的事件處理句柄。只寫 |
readyState | 返回當前請求的狀態,只讀. |
responseBody | 將回應信息正文以unsigned byte數組形式返回.只讀 |
responseStream | 以Ado Stream對象的形式返回響應信息。只讀 |
responseText | 將響應信息作爲字符串返回.只讀 |
responseXML | 將響應信息格式化爲Xml Document對象並返回,只讀 |
status | 返回當前請求的http狀態碼.只讀 |
statusText | 返回當前請求的響應行狀態,只讀 |
* 表示此屬性是W3C文檔對象模型的擴展.
方法
abort | 取消當前請求 |
getAllResponseHeaders | 獲取響應的所有http頭 |
getResponseHeader | 從響應信息中獲取指定的http頭 |
open | 創建一個新的http請求,並指定此請求的方法、URL以及驗證信息(用戶名/密碼) |
send | 發送請求到http服務器並接收回應 |
setRequestHeader | 單獨指定請求的某個http頭 |
onreadystatechange
指定當readyState屬性改變時的事件處理句柄
語法
oXMLHttpRequest.onreadystatechange = funcMyHandler;
如下的例子演示當XMLHTTPRequest對象的readyState屬性改變時調用HandleStateChange函數,當數據接收完畢後(readystate == 4)此頁面上的一個按鈕將被激活
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); } }
備註
此屬性只寫,爲W3C文檔對象模型的擴展.
readyState
返回XMLHTTP請求的當前狀態
語法
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"); } }
備註
變量,此屬性只讀,狀態用長度爲4的整型表示.定義如下:
0 (未初始化) | 對象已建立,但是尚未初始化(尚未調用open方法) |
1 (初始化) | 對象已建立,尚未調用send方法 |
2 (發送數據) | send方法已調用,但是當前的狀態及http頭未知 |
3 (數據傳送中) | 已接收部分數據,因爲響應及http頭不全,這時通過responseBody和responseText獲取部分數據會出現錯誤, |
4 (完成) | 數據接收完畢,此時可以通過通過responseBody和responseText獲取完整的迴應數據 |
responseBody
返回某一格式的服務器響應數據
語法
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格式表示直接從服務器返回的未經解碼的二進制數據。
responseStream
以Ado Stream對象的形式返回響應信息
語法
strValue = oXMLHttpRequest.responseStream;
備註
變量,此屬性只讀,以Ado Stream對象的形式返回響應信息。
responseText
將響應信息作爲字符串返回
語法
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,如果服務器返回的數據帶BOM(byte-order mark),XMLHTTP可以解碼任何UCS-2 (big or little endian)或者UCS-4 數據。注意,如果服務器返回的是xml文檔,此屬性並不處理xml文檔中的編碼聲明。你需要使用responseXML來處理。
responseXML
將響應信息格式化爲Xml Document對象並返回
語法
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,可以通過處理過的DOMDocument對象獲取錯誤信息。
status
返回當前請求的http狀態碼
語法
lValue = oXMLHttpRequest.status;var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.status);
返回值
長整形標準http狀態碼,定義如下:
Number | Description |
---|---|
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 |
備註
長整形,此屬性只讀,返回當前請求的http狀態碼,此屬性僅當數據發送並接收完畢後纔可獲取。
statusText
返回當前請求的響應行狀態
語法
strValue = oXMLHttpRequest.statusText;var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.statusText);
備註
字符串,此屬性只讀,以BSTR返回當前請求的響應行狀態,此屬性僅當數據發送並接收完畢後纔可獲取。
abort
取消當前請求
語法
oXMLHttpRequest.abort();
備註
調用此方法後,當前請求返回UNINITIALIZED 狀態。
getAllResponseHeaders
獲取響應的所有http頭
語法
strValue = oXMLHttpRequest.getAllResponseHeaders();var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml", false); xmlhttp.send(); alert(xmlhttp.getAllResponseHeaders());
輸出由web服務器返回的http頭信息:
Server:Microsoft-IIS/5.1 X-Powered-By:ASP.NET Date:Sat, 07 Jun 2003 23:23:06 GMT Content-Type:text/xml Accept-Ranges:bytes Last Modified:Sat, 06 Jun 2003 17:19:04 GMT ETag:"a0e2eeba4f2cc31:97f" Content-Length:9
備註
每個http頭名稱和值用冒號分割,並以/r/n結束。當send方法完成後纔可調用該方法。
getResponseHeader
從響應信息中獲取指定的http頭
語法
strValue = oXMLHttpRequest.getResponseHeader(bstrHeader);var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml", false); xmlhttp.send(); alert(xmlhttp.getResponseHeader("Server"));
輸出http頭中的server列:當前web服務器的版本及名稱。
備註
當send方法成功後纔可調用該方法。如果服務器返回的文檔類型爲"text/xml", 則這句話xmlhttp.getResponseHeader("Content-Type");將返回字符串"text/xml"。可以使用getAllResponseHeaders方法獲取完整的http頭信息。
open
創建一個新的http請求,並指定此請求的方法、URL以及驗證信息
語法
oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
參數
bstrMethod http方法,例如:POST、GET、PUT及PROPFIND。大小寫不敏感。
bstrUrl 請求的URL地址,可以爲絕對地址也可以爲相對地址。
varAsync[可選] 布爾型,指定此請求是否爲異步方式,默認爲true。如果爲真,當狀態改變時會調用onreadystatechange屬性指定的回調函數。
bstrUser[可選] 如果服務器需要驗證,此處指定用戶名,如果未指定,當服務器需要驗證時,會彈出驗證窗口。
bstrPassword[可選] 驗證信息中的密碼部分,如果用戶名爲空,則此值將被忽略。
下面的例子演示從服務器請求book.xml,並顯示其中的book字段。
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET","http://localhost/books.xml", false); xmlhttp.send(); var book = xmlhttp.responseXML.selectSingleNode("//book[@id='bk101']"); alert(book.xml);
備註
調用此方法後,可以調用send方法向服務器發送數據。
send
發送請求到http服務器並接收回應
語法
oXMLHttpRequest.send(varBody);
參數
varBody 欲通過此請求發送的數據。
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml", false); xmlhttp.send(); alert(xmlhttp.responseXML.xml);
備註
此方法的同步或異步方式取決於open方法中的bAsync參數,如果bAsync == False,此方法將會等待請求完成或者超時時纔會返回,如果bAsync == True,此方法將立即返回。
This method takes one optional parameter, which is the requestBody to use. The acceptable VARIANT input types are BSTR, SAFEARRAY of UI1 (unsigned bytes), IDispatch to an XML Document Object Model (DOM) object, and IStream *. You can use only chunked encoding (for sending) when sending IStream * input types. The component automatically sets the Content-Length header for all but IStream * input types.
如果發送的數據爲BSTR,則迴應被編碼爲utf-8, 必須在適當位置設置一個包含charset的文檔類型頭。
If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
如果發送的數據爲XML DOM object,則迴應將被編碼爲在xml文檔中聲明的編碼,如果在xml文檔中沒有聲明編碼,則使用默認的UTF-8。
If the input type is an IStream *, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
setRequestHeader
單獨指定請求的某個http頭
語法
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);
參數
bstrHeader 字符串,頭名稱。
bstrValue 字符串,值。
備註
如果已經存在已此名稱命名的http頭,則覆蓋之。此方法必須在open方法後調用。