XMLHttpRequest XMLHTTP

23
XMLHttpRequest 提户端http服务器器讯的协Example 下的下码是JScript中创建建个XMLHTTP对象并从服务器请求建个 XML文档。服务器XML文档并显示。 是在IE的浏浏器中new XMLHttpRequest()来创建对象器象下vbscript备注 提户端端器过XmlHttp对象(MSXML2.XMLHTTP.3.0)http服务器发送请求并使器使软XML文档对象Microsoft® XML Document Object Model (DOM)处理器应参考 XMLHTTPRequest成员 XMLHTTP对象参考 XMLHTTPRequest 对象 XMLHTTPRequest 成员 onreadystatechange readyState responseBody responseStream responseText responseXML status statusText abort getAllResponseHeaders getResponseHeader open send setRequestHeader var xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0"); xmlHttpReq.open("GET", "http://localhost/books.xml", false); xmlHttpReq.send(); alert(xmlHttpReq.responseText); var xmlHttpReq = new XMLHttpRequest(); xmlHttpReq.open("GET", "http://localhost/books.xml", false); xmlHttpReq.send(); alert(xmlHttpReq.responseText); Dim HttpReq As New MSXML2.XMLHTTP30 HttpReq.open "GET", "http://localhost/books.xml", False HttpReq.send MsgBox HttpReq.responseText

Transcript of XMLHttpRequest XMLHTTP

Page 1: XMLHttpRequest XMLHTTP

XMLHttpRequest

提提提户端端http服务器器讯的协协

Example

下下的下码是是JScript中创建建个XMLHTTP对象并从服务器请求建个

XML文档。服务器器器XML文档并显示。

是在IE的浏浏器中器器器器new XMLHttpRequest()来创建对象器象下象

vbscript象

备备备备注注注注

提户端端端器过XmlHttp对象(MSXML2.XMLHTTP.3.0)向http服务器发送请求并使器使软XML文档对象象

型Microsoft® XML Document Object Model (DOM)处理器应 。

参参参参考考考考

XMLHTTPRequest成员

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

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

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

Dim HttpReq As New MSXML2.XMLHTTP30 HttpReq.open "GET", "http://localhost/books.xml", False HttpReq.send MsgBox HttpReq.responseText

Page 2: XMLHttpRequest XMLHTTP

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式的禁止任何形式的禁止任何形式的禁止任何形式的转载转载转载转载!!!!

Page 3: XMLHttpRequest XMLHTTP

XMLHttpRequest 成成成成员员员员

属属属属性性性性

* 表表表属性性W3C文档对象象象象扩展.

方法方法方法方法

事件事件事件事件

参参参参考考考考

XMLHTTPRequest

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式象禁止任何形式象禁止任何形式象禁止任何形式象转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

onreadystatechange*指指当readyState属性性变变象事件处理理理理

只写

readyState 返返当前请求象状态,只读.

responseBody 将返应信信信文信unsigned byte数组形式返返.

只读

responseStream 信Ado Stream对象象形式返返响应信信理只读

responseText 将响应信信信为字字字返返.只读

responseXML将响应信信信式信为Xml Document对象并返

返,只读

status 返返当前请求象http状态态.只读

statusText 返返当前请求象响应行状态,只读

abort 取取当前请求

getAllResponseHeaders 获取响应象的的http头

getResponseHeader 从响应信信信获取指指象http头

open创建建个新象http请求,并指指表请求象方法求URL信以验验信信(用户名/密

态)

send 发送请求求http服务器并接接返应

setRequestHeader 单独指指请求象求个http头

Page 4: XMLHttpRequest XMLHTTP

onreadystatechange

指指当readyState属性性变变的的的处理理理

语语语语法法法法

Example

如如的如如如如当XMLHTTPRequest对象的readyState属性性变变变用HandleStateChange函数,当数

据据据据毕后后readystate == 4))页面面的面个按钮将被被被

备备备备注注注注

)属性性写,为W3C文档对象象象的扩展.

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

oXMLHttpRequest.onreadystatechange = funcMyHandler;

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); } }

Page 5: XMLHttpRequest XMLHTTP

参参参参考考考考

readyState 属性

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式的禁止任何形式的禁止任何形式的禁止任何形式的转载转载转载转载!!!!

Page 6: XMLHttpRequest XMLHTTP

readyState

返返XMLHTTP请求求当前状态

语语语语法法法法

Example

备备备备注注注注

变量量量属性性读量状态用长度为4求的的的的.定义如如如

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

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"); } }

0 (未未未

化)对象象象象量象象尚未未未化未尚未调用open方法方

1 (未未化) 对象象象象量尚未调用send方法

2 (发送数send方法象调用量象象当前求状态及http头未未

Page 7: XMLHttpRequest XMLHTTP

参参参参考考考考

open 方法

responseBody 属性

responseText 属性

send 方法

status 属性

statusText 属性

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式求禁止任何形式求禁止任何形式求禁止任何形式求转载转载转载转载!!!!

据)

3 (数据传送

中)

象已已已已数据量据为为为及http头不不量这这通过responseBody和responseText获取

已已数据会出现现现量

4 (完成) 数据已已完毕,量这可可通过通过responseBody和responseText获取完的求返为数据

Page 8: XMLHttpRequest XMLHTTP

responseBody

返返返返返返返返务器响响数据

语语语语法法法法

Example

备备备备注注注注

变量量量属性性读量,unsigned array返返格格格格从返务器返返返器经解码返的进制数据据

参参参参考考考考

responseStream 属性

responseText 属性

responseXML 属性

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形返返禁止任何形返返禁止任何形返返禁止任何形返返转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

strValue = oXMLHttpRequest.responseBody;

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.responseBody);

Page 9: XMLHttpRequest XMLHTTP

responseStream

以Ado Stream对象象象象象象响响信信

语语语语法法法法

备备备备注注注注

变量量量属性性读量以Ado Stream对象象象象象象响响信信信

参参参参考考考考

responseBody 属性

responseText 属性

responseXML 属性

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何象象象禁止任何象象象禁止任何象象象禁止任何象象象转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

strValue = oXMLHttpRequest.responseStream;

Page 10: XMLHttpRequest XMLHTTP

responseText

将响响信信信为字字字字字

语语语语法法法法

Example

备备备备注注注注

变量量量属性性读量将响响信信信为字字字字字字

XMLHTTP尝尝将响响信信信码为Unicode字字字量XMLHTTP默认将响响数据据编码定为UTF-8量,,,

务器字字据数据带BOM(byte-order mark)量XMLHTTP可可信码任任UCS-2 (big or little endian)或或

UCS-4 数据字注据量,,,务器字字据器xml文档量量属性并不处理xml文档中据编码声明字你需需需

用responseXML来处理字

参参参参考考考考

responseBody 属性

responseText 属性

responseXML 属性

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任任形式据禁止任任形式据禁止任任形式据禁止任任形式据转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

strValue = oXMLHttpRequest.responseText;

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

Page 11: XMLHttpRequest XMLHTTP

responseXML

将响响信信信信信为Xml Document对象并返返

语语语语法法法法

Example

备备备备注注注注

变量量量属性性读量将响响信信信信信为Xml Document对象并返返返返返响响数据据据据据据XML文

档量量属性性性据返返XMLDOMParseError量,,,过过理过据DOMDocument对象获取错错信信返

参参参参考考考考

responseBody 属性

responseStream 属性

responseText 属性

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形信据禁止任何形信据禁止任何形信据禁止任何形信据转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

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);

Page 12: XMLHttpRequest XMLHTTP

status

返返当前请求求http状态态

语语语语法法法法

Example

返返返返返返返返值值值值

长整整标准http状态态,,义如如如

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

lValue = oXMLHttpRequest.status;

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.status);

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

Page 13: XMLHttpRequest XMLHTTP

备备备备注注注注

长整整,整属性性读,返返当前请求求http状态态,整属性仅当当据发送并接接接毕后后后获 取取

参参参参考考考考

statusText 属性

send 方法

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何整式求禁止任何整式求禁止任何整式求禁止任何整式求转载转载转载转载!!!!

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

Page 14: XMLHttpRequest XMLHTTP

statusText

返返当前请求求响响行状态

语语语语法法法法

Example

备备备备注注注注

字字字字字属性性读字,BSTR返返当前请求求响响行状态,字属性仅当当据发送并接接接毕后后后获

取取

参参参参考考考考

status 属性

send 方法

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式求禁止任何形式求禁止任何形式求禁止任何形式求转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

strValue = oXMLHttpRequest.statusText;

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml", false); xmlhttp.send(); alert(xmlhttp.statusText);

Page 15: XMLHttpRequest XMLHTTP

abort

取取当前请求

语语语语法法法法

备备备备注注注注

调用用用法用用当前请求求求UNINITIALIZED 状态。

参参参参考考考考

readyState 属性

open 用法

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式的禁止任何形式的禁止任何形式的禁止任何形式的转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

oXMLHttpRequest.abort();

Page 16: XMLHttpRequest XMLHTTP

getAllResponseHeaders

获取响响的的的http头

语语语语法法法法

Example

输出出web服务器器器的http头信信信example:

备备备备注注注注

每个http头名称和值用用号分分信并以\r\n结束束当send方法方方方方方调用该 方法束

参参参参考考考考

send 方法

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 方员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

strValue = oXMLHttpRequest.getAllResponseHeaders();

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

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

Page 17: XMLHttpRequest XMLHTTP

getResponseHeader 方法

setRequestHeader 方法

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式的禁止任何形式的禁止任何形式的禁止任何形式的转载转载转载转载!!!!

Page 18: XMLHttpRequest XMLHTTP

getResponseHeader

从响响信信信获取取取取http头

语语语语法法法法

Example

输出http头信取server列列当前web服务器取器器器器称。

备备备备注注注注

当send方法方方方方方调用该方法。方方服务器器器取器档类型为"text/xml", 则则句话

xmlhttp.getResponseHeader("Content-Type");将器器返返返"text/xml"。方。。用

getAllResponseHeaders方法获取取取取http头信信。

参参参参考考考考

send 方法

getAllResponseHeaders 方法

setRequestHeader 方法

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式取禁止任何形式取禁止任何形式取禁止任何形式取转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 方员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

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"));

Page 19: XMLHttpRequest XMLHTTP

open

创建建个新新http请求求并指指指请求新求求求URL以以验验信信

语语语语求求求求

参数参数参数参数

bstrMethod http求求求方方方POST求GET求PUT以PROPFIND。。。写 不不不。

bstrUrl 请求新URL地地求地以为为为地地地地以为相为 地地。

varAsync[地选]

布尔型求指指指请求求求为异步求方求方认为true。方。为真求当当态改变变会调用

onreadystatechange属性指指新性调函数。

bstrUser[地选]

方。如务器器器验验求指处指指用户名求方。名指指求当如务器器器验验变求会弹出验验 窗窗。

bstrPassword[地选]

验验信信信新信码部部求方。用户名为空求则指值将 被被被。

Example

下下新方下下下从如务器请求book.xml,并显下示信新book 字字。

XMLHTTP为象参考

XMLHTTPRequest 为象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);

var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0 "); xmlhttp.open("GET","http://localhost/books.xml", fa lse); xmlhttp.send();

Page 20: XMLHttpRequest XMLHTTP

备备备备注注注注

调用指求求用求地以调用send求求方如务器发送数据。

参参参参考考考考

abort 求求

onreadystatechange 属性

XMLHTTPRequest 为象

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形方新禁止任何形方新禁止任何形方新禁止任何形方新转载转载转载转载!!!!

var book = xmlhttp.responseXML.selectSingleNode("// book[@id='bk101']"); alert(book.xml);

Page 21: XMLHttpRequest XMLHTTP

send

发送请求求http服务器并接接接应

语语语语法法法法

参数参数参数参数

varBody 欲欲过此请求发送送数 据据

Example

备备备备注注注注

此此法送此步或异步此方方决于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

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 求员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

oXMLHttpRequest.send(varBody);

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml", false); xmlhttp.send(); alert(xmlhttp.responseXML.xml);

Page 22: XMLHttpRequest XMLHTTP

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.

参参参参考考考考

open 此法

XMLHTTPRequest 对象

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形方送禁止任何形方送禁止任何形方送禁止任何形方送转载转载转载转载!!!!

Page 23: XMLHttpRequest XMLHTTP

setRequestHeader

单独指指请求求求个http头

语语语语法法法法

参数参数参数参数

bstrHeader 字字字字头名称 。

bstrValue 字字字字值。

备备备备注注注注

如如如经存存如存名称命名求http头字则覆盖之。存之法之须存open之法方调用。

参参参参考考考考

getAllResponseHeaders 之法

XMLHTTPRequest 对象

Copyright © 2005 xmlhttp.cn . All rights reserved.

禁止任何形式求禁止任何形式求禁止任何形式求禁止任何形式求转载转载转载转载!!!!

XMLHTTP对象参考

XMLHTTPRequest 对象

XMLHTTPRequest 成员

onreadystatechange

readyState

responseBody

responseStream

responseText

responseXML

status

statusText

abort

getAllResponseHeaders

getResponseHeader

open

send

setRequestHeader

oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);