XmlHttp對象參考

來源:互聯網
上載者:User

 

XmlHttp對象參考:

 

屬性:
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頭
abort

取消當前請求

文法
oXMLHttpRequest.abort();
備忘

調用此方法後,當前請求返回UNINITIALIZED 狀態。

getAllResponseHeaders

擷取響應的所有http頭

文法
strValue = oXMLHttpRequest.getAllResponseHeaders();
Example
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");xmlhttp.open("GET", "http://localhost/sample.xml", false);xmlhttp.send();alert(xmlhttp.getAllResponseHeaders());

輸出由web伺服器返回的http頭資訊,example:

Server:Microsoft-IIS/5.1X-Powered-By:ASP.NETDate:Sat, 07 Jun 2003 23:23:06 GMTContent-Type:text/xmlAccept-Ranges:bytesLast Modified:Sat, 06 Jun 2003 17:19:04 GMTETag:"a0e2eeba4f2cc31:97f"Content-Length:9
備忘

每個http頭名稱和值用冒號分割,並以/r/n結束。當send方法完成後才可調用該方法。

getResponseHeader

從響應資訊中擷取指定的http頭

文法
strValue = oXMLHttpRequest.getResponseHeader(bstrHeader);
Example
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頭資訊。


onreadystatechange

指定當readyState屬性改變時的事件處理控制代碼

文法
oXMLHttpRequest.onreadystatechange = funcMyHandler;
Example

如下的例子示範當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文件物件模型的擴充.

open

建立一個新的http請求,並指定此請求的方法、URL以及驗證資訊

文法
oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
參數

bstrMethod
http方法,例如:POST、GET、PUT及PROPFIND。大小寫不敏感。

bstrUrl
請求的URL地址,可以為絕對位址也可以為相對位址。

varAsync[可選]
布爾型,指定此請求是否為非同步方式,預設為true。如果為真,當狀態改變時會調用onreadystatechange屬性指定的回呼函數。

bstrUser[可選]
如果伺服器需要驗證,此處指定使用者名稱,如果未指定,當伺服器需要驗證時,會彈出驗證視窗。

bstrPassword[可選]
驗證資訊中的密碼部分,如果使用者名稱為空白,則此值將被忽略。

Example

下面的例子示範從伺服器請求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方法向伺服器發送資料。

 

readyState

返回XMLHTTP請求的目前狀態

文法
lValue = oXMLHttpRequest.readyState;
Example
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;
Example
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;
Example
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;
Example
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對象擷取錯誤資訊。

send

發送請求到http伺服器並接收回應

文法
oXMLHttpRequest.send(varBody);
參數

varBody
欲通過此請求發送的資料。

Example
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方法後調用。

status

返回當前請求的http狀態代碼

文法
lValue = oXMLHttpRequest.status;
Example
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;
Example
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");xmlhttp.open("GET", "http://localhost/books.xml", false);xmlhttp.send();alert(xmlhttp.statusText);
備忘

字串,此屬性唯讀,以BSTR返回當前請求的響應行狀態,此屬性僅當資料發送並接收完畢後才可擷取。

XMLHttpRequest

提供用戶端同http伺服器通訊的協議

Example

下面的代碼是在JScript中建立一個XMLHTTP對象並從伺服器請求一個XML文檔。伺服器返回XML文檔並顯示。

var xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");xmlHttpReq.open("GET", "http://localhost/books.xml", false);xmlHttpReq.send();alert(xmlHttpReq.responseText);

在非IE的瀏覽器中,需要用new XMLHttpRequest()來建立對象,如下:

var xmlHttpReq = new XMLHttpRequest();xmlHttpReq.open("GET", "http://localhost/books.xml", false);xmlHttpReq.send();alert(xmlHttpReq.responseText);

vbscript:

Dim HttpReq As New MSXML2.XMLHTTP30HttpReq.open "GET", "http://localhost/books.xml", FalseHttpReq.sendMsgBox 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頭

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.