XMLHTTP object reference:
Attribute:
| Onreadystatechange * |
Specifies the event processing handle when the readystate attribute changes. Write only |
| Readystate |
Returns the status of the current request, read-only. |
| Responsebody |
Returns the response body in the unsigned byte array. Read-Only |
| Responsestream |
Return the response information in the form of an ADO Stream object. Read-Only |
| Responsetext |
Returns the response information as a string. Read-Only |
| Responsexml |
Format the response information as an XML Document Object and return it. Read-Only |
| Status |
Returns the HTTP status code of the current request. Read-Only |
| Statustext |
Returns the response line status of the current request, read-only |
* Indicates that this attribute is an extension of the W3C Document Object Model.
Method:
| Abort |
Cancel current request |
| GetAllResponseHeaders |
Get all HTTP headers of the response |
| GetResponseHeader |
Obtain the specified HTTP header from the response information |
| Open |
Create a new HTTP request and specify the request method, URL, and authentication information (User Name/password) |
| Send |
Send a request to the HTTP server and receive a response |
| SetRequestHeader |
Specify an HTTP header of the Request separately |
Abort
Cancel current request
Syntax
oXMLHttpRequest.abort();
Remarks
After this method is called, the current request returns the uninitialized status.
GetAllResponseHeaders
Get all HTTP headers of the response
Syntax
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());
The HTTP header returned by the Web server, 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
Remarks
Each HTTP header name and value are separated by a colon and end with/R/n. This method is called only after the send method is complete.
GetResponseHeader
Obtain the specified HTTP header from the response information
Syntax
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"));
Output the server column in the HTTP header: version and name of the current web server.
Remarks
This method is called only when the send method is successful. If the document type returned by the server is "text/XML", XMLHTTP. getResponseHeader ("Content-Type"); returns the string "text/XML ". You can use the getAllResponseHeaders method to obtain the complete HTTP header information.
Onreadystatechange
Specifies the event handling handle when the readystate attribute changes.
Syntax
oXMLHttpRequest.onreadystatechange = funcMyHandler;
Example
The following example shows how to call the handlestatechange function when the readystate attribute of the XMLHTTPRequest object is changed. After the data is received (readystate = 4), a button on this page will be activated.
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); }}Remarks
This attribute is only written and is an extension of the W3C Document Object Model.
Open
Create an HTTP request and specify the method, URL, and authentication information of the request.
Syntax
oXMLHttpRequest.open(bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword);
Parameters
Bstrmethod
HTTP methods, such as post, get, put, and PROPFIND. Case Insensitive.
Bstrurl
The requested URL address, which can be an absolute or relative address.
Varasync [Optional]
Boolean: Specifies whether the request is asynchronous. The default value is true. If it is true, the callback function specified by the onreadystatechange attribute is called when the status changes.
Bstruser [Optional]
If the server needs to be verified, the user name is specified here. If not, the verification window is displayed when the server needs to be verified.
Bstrpassword [Optional]
The password section in the verification information. If the user name is empty, this value is ignored.
Example
The following example shows how to request book. XML from the server and display the book field.
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);Remarks
After calling this method, you can call the send method to send data to the server.
Readystate
Returns the current status of the XMLHTTP request.
Syntax
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"); }}Remarks
Variable. This attribute is read-only and the status is represented by an integer with a length of 4. The definition is as follows:
| 0 (not initialized) |
Object created but not initialized (open method not called) |
| 1 (initialization) |
The object has been created and the send method has not been called |
| 2 (send data) |
The send method has been called, but the current status and HTTP header are unknown. |
| 3 (data transmission in progress) |
Some data has been received. Because the response and HTTP headers are incomplete, an error occurs when you obtain some data through responsebody and responsetext, |
| 4 (finished) |
After receiving the data, you can use responsebody and responsetext to obtain the complete response data. |
Responsebody
Returns server response data in a certain format.
Syntax
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);Remarks
Variable. This attribute is read-only and represents undecoded binary data directly returned from the server in unsigned array format.
Responsestream
Return response information in the form of an ADO Stream Object
Syntax
strValue = oXMLHttpRequest.responseStream;
Remarks
Variable. This attribute is read-only and returns response information in the form of an ADO Stream object.
Responsetext
Returns response information as a string.
Syntax
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);Remarks
Variable, which is read-only and returns response information as a string.
XMLHTTP tries to decode the response information as a unicode string, and XMLHTTP sets the encoding of the response data as a UTF-8 by default. If the data returned by the server includes BOM (byte-order mark ), XMLHTTP can decode any UCS-2 (big or little endian) or UCS-4 data. Note: If the server returns an XML document, this attribute does not process the encoding declaration in the XML document. You need to use responsexml for processing.
Responsexml
Format the response information as an XML Document Object and return
Syntax
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);Remarks
Variable. This attribute is read-only, and the response information is formatted as an XML Document Object and returned. If the response data is not a valid XML document, this attribute does not return xmldomparseerror. You can obtain error information through the processed domdocument object.
Send
Send a request to the HTTP server and receive a response
Syntax
oXMLHttpRequest.send(varBody);
Parameters
Varbody
Data to be sent through this request.
Example
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");xmlhttp.open("GET", "http://localhost/sample.xml", false);xmlhttp.send();alert(xmlhttp.responseXML.xml);Remarks
The synchronous or Asynchronous Method of this method depends on the basync parameter in the open method. If basync = false, this method will not return until the request is completed or times out. If basync = true, this method returns immediately.
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.
If the sent data is BSTR, the response is encoded as UTF-8, and a document type header containing charset must be set in the appropriate position.
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.
If the data sent is an xml dom object, the response is encoded as the encoding declared in the XML document, and if no encoding is declared in the XML document, the default UTF-8 is used.
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
Specify an HTTP header of the Request separately
Syntax
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);
Parameters
Bstrheader
String, header name.
Bstrvalue
String, value.
Remarks
If an HTTP header with the name already exists, overwrite it. This method must be called after the open method.
Status
Returns the HTTP status code of the current request.
Syntax
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);Return Value
Standard HTTP status code for long shaping, which is defined as follows:
| 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 |
Remarks
Long Integer. This attribute is read-only and returns the HTTP status code of the current request. This attribute can be obtained only after data is sent and received.
Statustext
Returns the response line status of the current request.
Syntax
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);Remarks
String. This attribute is read-only and returns the status of the response line of the current request with BSTR. This attribute can be obtained only after data is sent and received.
XMLHttpRequest
Provides the protocol for communication between the client and the HTTP server.
Example
The following code creates an XMLHTTP object in JScript and requests an XML document from the server. The server returns and displays the XML document.
var xmlHttpReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");xmlHttpReq.open("GET", "http://localhost/books.xml", false);xmlHttpReq.send();alert(xmlHttpReq.responseText);
In a non-IE browser, you need to use new XMLHttpRequest () to create an object, as shown below:
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
Remarks
The client can send a request to the HTTP server through the XMLHTTP object (msxml2.xmlhttp. 3.0) and process the response using the Microsoft XML Document Object Model (DOM.
XMLHttpRequest member attributes
| Onreadystatechange * |
Specifies the event processing handle when the readystate attribute changes. Write only |
| Readystate |
Returns the status of the current request, read-only. |
| Responsebody |
Returns the response body in the unsigned byte array. Read-Only |
| Responsestream |
Return the response information in the form of an ADO Stream object. Read-Only |
| Responsetext |
Returns the response information as a string. Read-Only |
| Responsexml |
Format the response information as an XML Document Object and return it. Read-Only |
| Status |
Returns the HTTP status code of the current request. Read-Only |
| Statustext |
Returns the response line status of the current request, read-only |
* Indicates that this attribute is an extension of the W3C Document Object Model.
Method
| Abort |
Cancel current request |
| GetAllResponseHeaders |
Get all HTTP headers of the response |
| GetResponseHeader |
Obtain the specified HTTP header from the response information |
| Open |
Create a new HTTP request and specify the request method, URL, and authentication information (User Name/password) |
| Send |
Send a request to the HTTP server and receive a response |
| SetRequestHeader |
Specify an HTTP header of the Request separately |