XmlHttp对象参考(2) 方法

本文详细介绍了XMLHttpRequest对象的主要方法,包括取消请求、获取响应头部信息、创建和发送HTTP请求等,并提供了多个实例帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

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头信息。

 

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方法向服务器发送数据。

 

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方法后调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值