MLHttpRequest.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
方法向服务器发送数据。
|
XMLHttpRequest.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.
|
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
|
大多数情况下,开发人员并不需要关心所的状态,一般只关心返回的状态是不是“OK”状态即可,这时只要判断XMLHttpRequest对象的status属性的值是否为200即可。
除此之外,当客户端向服务器发出一个请求之后,服务器会向客户端作出五次响应,并且每一次响应都会被客户端所获取。这五次服务器响应代表了返回XMLHTTP请求的当前状态,我们可以通过XMLHttpRequest对象的readyState属性来获取该状态代码。状态代码代表示的意义如下:
0 (
未初始化
)
|
对象已建立,但是尚未初始化(尚未调用
open
方法)
|
1 (
初始化
)
|
对象已建立,尚未调用
send
方法
|
2 (
发送数据
)
|
send
方法已调用,但是当前的状态及
http
头未知
|
3 (
数据传送中
)
|
已接收部分数据,因为响应及
http
头不全,这时通过
responseBody
和
responseText
获取部分数据会出现错误,
|
4 (
完成
)
|
数据接收完毕
,
此时可以通过通过
responseBody
和
responseText
获取完整的回应数据
|