Attributes and methods of the XMLHTTPRequest object

Source: Internet
Author: User
XMLHttpRequest

Provides the sameHTTPServer Communication Protocol

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

The client can use the XMLHTTP object (msxml2.xmlhttp. 3.0)HTTPThe server sends a request and uses MicrosoftXMLDocument Object Model Microsoft?XMLThe Document Object Model (DOM) processes the response.

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 responseXMLDocument Object and return, read-only
Status ReturnsHTTPStatus Code. 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 response itemsHTTPHeader
GetResponseHeader Obtain the specifiedHTTPHeader
Open Create a newHTTPRequest, and specify the request method, URL, and authentication information (User Name/password)
Send Send requestHTTPServer and receive response
SetRequestHeader Specify a specific requestHTTPHeader
Onreadystatechange

Specifies the event handling handle when the readystate attribute changes.

Syntax
oXMLHttpRequest.onreadystatechange = funcMyHandler; 

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.

Readystate

Returns the current status of the XMLHTTP request.

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

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 andHTTPUnknown Header
3 (data transmission in progress) Some data has been received because of the response andHTTPIf the header is incomplete, an error occurs when you use responsebody and responsetext to obtain some data,
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; 
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; 
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 returnsXMLDocument, this attribute is not processedXMLThe encoding declaration in this document. You need to use responsexml for processing.

Responsexml

Format the responseXMLDocument Object and return

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

Variable. This attribute is read-only and the response is formattedXMLDocument Object and return. If the response data is not validXMLThis attribute does not return xmldomparseerror. You can obtain error information through the domdocument object that has been processed.

Status

ReturnsHTTPStatus 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 Integer StandardHTTPStatus Code, 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

HTTPVersion Not Supported

Remarks

Long Integer. This attribute is read-only and returnsHTTPStatus Code. 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; 
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.

Abort

Cancel current request

Syntax
oXMLHttpRequest.abort(); 
Remarks

After this method is called, the current request returns the uninitialized status.

GetAllResponseHeaders

Get all response itemsHTTPHeader

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

The output is returned by the Web server.HTTPHeader 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

EachHTTPThe header names and values are separated by colons and end with/R/n. This method is called only after the send method is complete.

GetResponseHeader

Obtain the specifiedHTTPHeader

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

OutputHTTPThe server column in the header: The 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", Then XMLHTTP. getResponseHeader (" Content-Type "); will return the string" text/XML". You can use the getAllResponseHeaders method to obtain the completeHTTPHeader information.

Open

Create a newHTTPRequest, and specify the request method, URL, and authentication information

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

Bstrmethod
HTTPMethods, 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, it is called when the status changesOnreadystatechangeThe callback function specified by the property.

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.

The following example shows how to request a book from the server.XMLAnd the book field is displayed.

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 requestHTTPServer and receive response

Syntax
oXMLHttpRequest.send(varBody); 
Parameters

Varbody
Data to be sent through this request.

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 toXMLDocument 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 sent data isXMLDOM object, the response will be encoded inXMLThe encoding stated in the document. IfXMLIf no encoding is declared in the 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 a specific requestHTTPHeader

Syntax
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue); 
Parameters

Bstrheader
String, header name.

Bstrvalue
String, value.

Remarks

If the specified name already existsHTTP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.