XMLHttpRequest


To provide client server communications with the http protocol

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

Remarks


XmlHttp client object can (MSXML2.XMLHTTP.3.0) to send the request to the http server and use the Microsoft XML Document Object Model Microsoft ® XML Document Object Model (DOM) in response to deal with.

XMLHttpRequest members

Properties




onreadystatechange When the readyState attribute to change the designation of the event handler when the handler. Write only
readyState Request to return to the current state of read-only.
responseBody The body will respond to information in the form of unsigned byte array to return. Read-only
responseStream Ado Stream object to return the form to respond to information. Read-only
responseText Will respond to information as a string to return. Read-only
responseXML In response to information will be formatted as Xml Document object and return to, read-only
status Return the current http request status code. Read-only
statusText In response to a request to return to the current line, and read-only



* Indicates that this attribute is the W3C Document Object Model extension.

Methods




abort Cancel current request
getAllResponseHeaders Access to all the http response header
getResponseHeader In response to information obtained from the specified http header
open Create a new http request, and specify the request method, URL and authentication information (username / password)
send Send a request to the http server and receive response
setRequestHeader Designated a separate http request header



onreadystatechange


When the readyState attribute to change the designation of the event handler when the handler

Syntax

oXMLHttpRequest.onreadystatechange = funcMyHandler; 

The following examples demonstrate the XMLHTTPRequest object when the readyState property changes HandleStateChange function call, when the data reception after (readystate == 4) on this page a button 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 for the W3C Document Object Model extension.

readyState


XMLHTTP request to return to the current state of

Syntax

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

Remarks


Variables, this property read-only, state to use the length of the integer that is 4. Are defined as follows:



0 (uninitialized) Object has been established, but has not yet been initialized (not yet open method call)
1 (Initialization) Object has been established, call the send method has not yet been
2 (send data) send method has been called, but the current state and the first unknown http
3 (data transmission) Has received part of the data, because the first http response and incomplete, then responseText access through responseBody and part of the data error,
4 (completed) Data received after this time can responseBody and responseText through access to a complete response to the data



responseBody


Back to the server in response to a data format

Syntax

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

Remarks


Variables, this read-only attribute to unsigned array format directly from the server to return the binary data without decoding.

responseStream


Ado Stream object to the form of return in response to information

Syntax

strValue = oXMLHttpRequest.responseStream; 

Remarks


Variables, this read-only attribute to the form of Ado Stream object to return in response to information.

responseText


Will respond to information as a string return

Syntax

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

Remarks


Variables, this read-only attributes will be returned to respond to information as a string.
XMLHTTP response to try to decode the information for the Unicode string, XMLHTTP default will be to respond to data encoded as UTF-8, if the server to return data with BOM (byte-order mark), XMLHTTP can decode any UCS-2 (big or little endian ) or UCS-4 data. Note that if the server returned to the xml document, this attribute xml document does not deal with the encoding declaration. You need to use responseXML To deal with it.

responseXML


In response to information will be formatted as Xml Document object and returns

Syntax

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

Remarks


Variables, this read-only attribute will be formatted as a response to the information and return Xml Document object. If the response data is not a valid XML document, this attribute does not return XMLDOMParseError, can be processed to obtain error message DOMDocument object.

status


Return the current http request status code

Syntax

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

Return value


Long plastic standard http status code, 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 plastic, this read-only attribute, return the current http request status code, this attribute only if the data sent and received in order to obtain after.

statusText


In response to a request to return to the current line status

Syntax

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

Remarks


String, read-only attribute this to the BSTR return to the current response to the request of the state line, this attribute only if the data sent and received in order to obtain after.

abort


Cancel current request

Syntax

oXMLHttpRequest.abort(); 

Remarks


After calling this method, the current status of the request to return to UNINITIALIZED.

getAllResponseHeaders


Access to all the http response header

Syntax

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

Output by the web server to return to the http header information:

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 

Remarks


Http header for each name and value divided by a colon, and \ r \ n end. When the send method calls this method before.

getResponseHeader


In response to information obtained from the specified http header

Syntax

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

Http header output of the server list: the current version of web server and the name.

Remarks


When the send method can only be called after the success of the method. If the server returned to the document type to "text / xml", then this sentence xmlhttp.getResponseHeader ( "Content-Type"); will return the string "text / xml". GetAllResponseHeaders method can be used to obtain a complete http header information.

open


Create a new http request, and specify the request method, URL and authentication information

Syntax

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

Parameters


bstrMethod
http methods, such as: POST, GET, PUT and PROPFIND. Case insensitive.

bstrUrl
URL address of the request can be can be an absolute address for the relative address.

varAsync [optional]
Boolean to specify whether the request for asynchronous mode, default is true. If true, when the state changes will call onreadystatechange property of the specified callback function.

bstrUser [optional]
If the server requires authentication, specify user name here, If you do not specify when the server requires authentication, the authentication window will pop up.

bstrPassword [optional]
Password to verify some information, if the user name space, then this value will be ignored.

The following examples demonstrate the request from the server book.xml, 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.

send


Send a request to the http server and receive response

Syntax

oXMLHttpRequest.send(varBody); 

Parameters


varBody
For this request to send data.

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

Remarks


This method depends on the synchronous or asynchronous methods open the bAsync parameters, if bAsync == False, this method will be completed or timed out waiting for request to return when, if bAsync == True, this method will return 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 data sent BSTR, the response is encoded as utf-8, must be set at an appropriate location that contains the Document Type charset header.

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 XML DOM object, the response will be coded in the xml document encoding declaration, if not in the xml document encoding declaration, then use the default 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


Designated a separate http request header

Syntax

oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue); 

Parameters


bstrHeader
String, the first name.

bstrValue
String value.



Remarks


If you already have the name of the http header, then the coverage of. This method must be called after the open approach.