(http://www.tkk7.com/zhangrenquan/articles/57845.html)
xmlhttp:onreadystatechange屬性
onreadystatechange
指定當(dāng)readyState屬性改變時(shí)的事件處理句柄
語(yǔ)法
oXMLHttpRequest.onreadystatechange = funcMyHandler;
Example
如下的例子演示當(dāng)XMLHTTPRequest對(duì)象的readyState屬性改變時(shí)調(diào)用HandleStateChange函數(shù),當(dāng)數(shù)據(jù)接收完畢后(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);
}
}
?
XMLHttpRequest對(duì)象
XMLHttpRequest
提供客戶(hù)端同http服務(wù)器通訊的協(xié)議
Example
下面的代碼是在JScript中創(chuàng)建一個(gè)XMLHTTP對(duì)象并從服務(wù)器請(qǐng)求一個(gè)XML文檔。服務(wù)器返回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()來(lái)創(chuàng)建對(duì)象,如下:
var xmlHttpReq = new XMLHttpRequest();
xmlHttpReq.open("GET", "http://localhost/books.xml", false);
xmlHttpReq.send();
alert(xmlHttpReq.responseText);
vbscript:
Dim HttpReq As New MSXML2.XMLHTTP30
HttpReq.open "GET", "http://localhost/books.xml", False
HttpReq.send
MsgBox HttpReq.responseText
備注
客戶(hù)端可以通過(guò)XmlHttp對(duì)象(MSXML2.XMLHTTP.3.0)向http服務(wù)器發(fā)送請(qǐng)求并使用微軟XML文檔對(duì)象模型Microsoft? XML Document Object Model (DOM)處理回應(yīng)。
xmlhttp:readyState屬性
readyState
返回XMLHTTP請(qǐng)求的當(dāng)前狀態(tài)
語(yǔ)法
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");
}
}
備注
變量,此屬性只讀,狀態(tài)用長(zhǎng)度為4的整型表示.定義如下:
0 (未初始化) 對(duì)象已建立,但是尚未初始化(尚未調(diào)用open方法)
1 (初始化) 對(duì)象已建立,尚未調(diào)用send方法
2 (發(fā)送數(shù)據(jù)) send方法已調(diào)用,但是當(dāng)前的狀態(tài)及http頭未知
3 (數(shù)據(jù)傳送中) 已接收部分?jǐn)?shù)據(jù),因?yàn)轫憫?yīng)及http頭不全,這時(shí)通過(guò)responseBody和responseText獲取部分?jǐn)?shù)據(jù)會(huì)出現(xiàn)錯(cuò)誤,
4 (完成) 數(shù)據(jù)接收完畢,此時(shí)可以通過(guò)通過(guò)responseBody和responseText獲取完整的回應(yīng)數(shù)據(jù)
?
xmlhttp:responsebody屬性
responseBody
返回某一格式的服務(wù)器響應(yīng)數(shù)據(jù)
語(yǔ)法
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);
備注
變量,此屬性只讀,以u(píng)nsigned array格式表示直接從服務(wù)器返回的未經(jīng)解碼的二進(jìn)制數(shù)據(jù)。
參考
responseStream 屬性
responseText 屬性
?
xmlhttp:responsestream屬性
responseStream
以Ado Stream對(duì)象的形式返回響應(yīng)信息
語(yǔ)法
strValue = oXMLHttpRequest.responseStream;
備注
變量,此屬性只讀,以Ado Stream對(duì)象的形式返回響應(yīng)信息。
?
xmlhttp:responsetext屬性
responseText
將響應(yīng)信息作為字符串返回
語(yǔ)法
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);
備注
變量,此屬性只讀,將響應(yīng)信息作為字符串返回。
XMLHTTP嘗試將響應(yīng)信息解碼為Unicode字符串,XMLHTTP默認(rèn)將響應(yīng)數(shù)據(jù)的編碼定為UTF-8,如果服務(wù)器返回的數(shù)據(jù)帶BOM(byte -order mark),XMLHTTP可以解碼任何UCS-2 (big or little endian)或者UCS-4 數(shù)據(jù)。注意,如果服務(wù)器返回的是xml文檔,此屬性并不處理xml文檔中的編碼聲明。你需要使用responseXML來(lái)處理。
?
xmlhttprequest:responsexml成員
responseXML
將響應(yīng)信息格式化為Xml Document對(duì)象并返回
語(yǔ)法
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);
備注
變量,此屬性只讀,將響應(yīng)信息格式化為Xml Document對(duì)象并返回。如果響應(yīng)數(shù)據(jù)不是有效的XML文檔,此屬性本身不返回XMLDOMParseError,可以通過(guò)處理過(guò)的DOMDocument對(duì)象獲取錯(cuò)誤信息。
?
xmlhttprequest對(duì)象:status成員
status
返回當(dāng)前請(qǐng)求的http狀態(tài)碼
語(yǔ)法
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);
返回值
長(zhǎng)整形標(biāo)準(zhǔn)http狀態(tài)碼,定義如下:
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
備注
長(zhǎng)整形,此屬性只讀,返回當(dāng)前請(qǐng)求的http狀態(tài)碼,此屬性?xún)H當(dāng)數(shù)據(jù)發(fā)送并接收完畢后才可獲取。
?
xmlhttprequest:statusText成員
statusText
返回當(dāng)前請(qǐng)求的響應(yīng)行狀態(tài)
語(yǔ)法
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返回當(dāng)前請(qǐng)求的響應(yīng)行狀態(tài),此屬性?xún)H當(dāng)數(shù)據(jù)發(fā)送并接收完畢后才可獲取。
?
xmlhttp:abort方法
abort
取消當(dāng)前請(qǐng)求
語(yǔ)法
oXMLHttpRequest.abort();
備注
調(diào)用此方法后,當(dāng)前請(qǐng)求返回UNINITIALIZED 狀態(tài)。
?
xmlhttp:getallresponseheaders方法
getallresponseheaders
獲取響應(yīng)的所有http頭
語(yǔ)法
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服務(wù)器返回的http頭信息,example:
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
備注
每個(gè)http頭名稱(chēng)和值用冒號(hào)分割,并以\r\n結(jié)束。當(dāng)send方法完成后才可調(diào)用該方法。
?
xmlhttp:getResponseHeader方法
getResponseHeader
從響應(yīng)信息中獲取指定的http頭
語(yǔ)法
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列:當(dāng)前web服務(wù)器的版本及名稱(chēng)。
備注
當(dāng)send方法成功后才可調(diào)用該方法。如果服務(wù)器返回的文檔類(lèi)型為"text/xml", 則這句話(huà)xmlhttp.getResponseHeader("Content-Type");將返回字符串"text/xml"。可以使用 getAllResponseHeaders方法獲取完整的http頭信息。
?
xmlhttp:open方法
open
創(chuàng)建一個(gè)新的http請(qǐng)求,并指定此請(qǐng)求的方法、URL以及驗(yàn)證信息
語(yǔ)法
oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
參數(shù)
bstrMethod
http方法,例如:POST、GET、PUT及PROPFIND。大小寫(xiě)不敏感。
bstrUrl
請(qǐng)求的URL地址,可以為絕對(duì)地址也可以為相對(duì)地址。
varAsync[可選]
布爾型,指定此請(qǐng)求是否為異步方式,默認(rèn)為true。如果為真,當(dāng)狀態(tài)改變時(shí)會(huì)調(diào)用onreadystatechange屬性指定的回調(diào)函數(shù)。
bstrUser[可選]
如果服務(wù)器需要驗(yàn)證,此處指定用戶(hù)名,如果未指定,當(dāng)服務(wù)器需要驗(yàn)證時(shí),會(huì)彈出驗(yàn)證窗口。
bstrPassword[可選]
驗(yàn)證信息中的密碼部分,如果用戶(hù)名為空,則此值將被忽略。
Example
下面的例子演示從服務(wù)器請(qǐng)求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("http://book[@id=''bk101'']");
alert(book.xml);
備注
調(diào)用此方法后,可以調(diào)用send方法向服務(wù)器發(fā)送數(shù)據(jù)。
?
xmlhttp:send方法
send
發(fā)送請(qǐng)求到http服務(wù)器并接收回應(yīng)
語(yǔ)法
oXMLHttpRequest.send(varBody);
參數(shù)
varBody
欲通過(guò)此請(qǐng)求發(fā)送的數(shù)據(jù)。
Example
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
xmlhttp.open("GET", "http://localhost/sample.xml", false);
xmlhttp.send();
alert(xmlhttp.responseXML.xml);
備注
此方法的同步或異步方式取決于open方法中的bAsync參數(shù),如果bAsync == False,此方法將會(huì)等待請(qǐng)求完成或者超時(shí)時(shí)才會(huì)返回,如果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.
如果發(fā)送的數(shù)據(jù)為BSTR,則回應(yīng)被編碼為utf-8, 必須在適當(dāng)位置設(shè)置一個(gè)包含charset的文檔類(lèi)型頭。
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.
如果發(fā)送的數(shù)據(jù)為XML DOM object,則回應(yīng)將被編碼為在xml文檔中聲明的編碼,如果在xml文檔中沒(méi)有聲明編碼,則使用默認(rèn)的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.
?
xmlhttp:setrequestheader方法
setRequestHeader
單獨(dú)指定請(qǐng)求的某個(gè)http頭
語(yǔ)法
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);
參數(shù)
bstrHeader
字符串,頭名稱(chēng)。
bstrValue
字符串,值。
備注
如果已經(jīng)存在已此名稱(chēng)命名的http頭,則覆蓋之。此方法必須在open方法后調(diào)用。
?
xmlhttp的請(qǐng)求同步和異步、方法的get和post
http://www.niceidea.org/post/xmlhttp_true_false_post_get.html
看看open方法的另外幾個(gè)參數(shù)。
.open http-method, url, async, userID, password (后面是帳號(hào)和密碼,在禁止匿名訪(fǎng)問(wèn)的http頁(yè)面中,需要用戶(hù)名和口令)
首先看看異步處理方式。
其中async是一個(gè)布爾值。如果是異步通信方式(true),客戶(hù)機(jī)就不等待服務(wù)器的響應(yīng);如果是同步方式(false),客戶(hù)機(jī)就要等到服務(wù)器返回消息后才去執(zhí)行其他操作。我們需要根據(jù)實(shí)際需要來(lái)指定同步方式,在某些頁(yè)面中,可能會(huì)發(fā)出多個(gè)請(qǐng)求,甚至是有組織有計(jì)劃有隊(duì)形大規(guī)模的高強(qiáng)度的request,而后一個(gè)是會(huì)覆蓋前一個(gè)的,這個(gè)時(shí)候當(dāng)然要指定同步方式:Flase。
//niceidea 簽名留念
首先看看method,方法。
一個(gè)標(biāo)準(zhǔn)的http請(qǐng)求頭:
7/8/99 10:27:16 Sent GET /Store/Download.asp HTTP/1.1
Accept: application/msword, application/vnd.ms-execl, application/vnd.ms-
powerpoint, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-
comet, */*
Accept-Language: en-us
Encoding: gzip, deflate
Referer: http://ww.wrox.com/main_menu.asp
Cookie: VisitCount=2&LASTDATE=6%2F4%2F99+10%3A10%3A13+AM
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)
Host: 212.250.238.67
Connection: Keep-Alive
很容易看懂,其中的method包括post/get/put等。對(duì)應(yīng)的主要是對(duì)于form即表單元素的處理方法。
當(dāng)mothod值為get時(shí),表單將附加在action頁(yè)面的url中;如果頁(yè)面是asp的,將會(huì)request.querystring中獲得;
如果是post,將會(huì)在request.form中獲得,
對(duì)應(yīng)與put方法的表單寫(xiě)法是:form method="POST" enctype='multipart/form-data'
主要用于上傳文件。
使用那種方法取決于服務(wù)端