XMLHTTP and Ajax Reference Manual

Source: Internet
Author: User
Tags microsoft iis
XMLHttpRequest
Provides the protocol for communication between the client and the HTTP server.

Dim httpreq as new msxml2.xmlhttp30
Httpreq. Open "get", "http: // localhost/books. xml", false
Httpreq. Send
Msgbox 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
Attribute

Onreadystatechange specifies the event handling 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 form of an unsigned byte array. Read-Only
Responsestream returns 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 into an XML Document Object and return it. Read-Only
Status returns the HTTP status code of the current request. Read-Only
Statustext returns the status of the response line of the current request, read-only

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

Method

Abort cancels the current request
GetAllResponseHeaders
GetResponseHeader obtains the specified HTTP header from the response information
Open creates a new HTTP request and specifies the request method, URL, and authentication information (User Name/password)
Send request to HTTP server and receive response
SetRequestHeader separately specifies an HTTP header of the request

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) the object has been created, but has not been initialized (the open method has not been 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 (in data transmission) Some data has been received. Because the response and HTTP headers are incomplete, an error occurs when retrieving part of data through responsebody and responsetext,
4. 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 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;

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.

Status
Returns the HTTP status code of the current request.

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

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 HTTP headers of the response

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 HTTP header information returned by the Web server:

Server: Microsoft-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
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 );

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.

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.

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.

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.

XMLHTTP = new activexobject ("msxml2.xmlhttp. 3.0 ");
XMLHTTP. Open ("get", "http://hi.baidu.com/ailiss", 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.

XML file

X = new activexobject ("Microsoft. XMLHTTP ")
X. Open ("get", "http://keqiji.org/rss/listings.xml", false)
X. Send ()
If (X. Status = 200)
{
D = x. responsexml
C ="



"
C + ="

"
C + ="

"
}
C + ="

"Nodes = D. selectnodes (" // records ") for (I = 0; I
"+ Nodes [0]. childnodes [I]. nodename + ":
"+ Subnodes [J]. childnodes [1]. Text +"
"+ Subnodes [J]. childnodes [2]. Text +"

"
Document. All. T. innerhtml = C
}

Post

Function xhconn ()
{
VaR XMLHTTP, bcomplete = false;
Try {XMLHTTP = new activexobject ("msxml2.xmlhttp ");}
Catch (e) {try {XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");}
Catch (e) {try {XMLHTTP = new XMLHttpRequest ();}
Catch (e) {XMLHTTP = false ;}}}
If (! XMLHTTP) return NULL;
This. Connect = function (Surl, smethod, svars, fndone)
{
If (! XMLHTTP) return false;
Bcomplete = false;
Smethod = smethod. touppercase ();

try {
If (smethod = "get")
{< br> XMLHTTP. Open (smethod, Surl + "? "+ Svars, true);
svars =" ";
}< br> else
{< br> XMLHTTP. open (smethod, Surl, true);
XMLHTTP. setRequestHeader ("method", "Post" + Surl + "HTTP/1.1");
XMLHTTP. setRequestHeader ("Content-Type",
"application/X-WWW-form-urlencoded");
}< br> XMLHTTP. onreadystatechange = function () {
If (XMLHTTP. readystate = 4 &&! Bcomplete)
{< br> bcomplete = true;
fndone (XMLHTTP);
};< br> XMLHTTP. send (svars);
}< br> catch (z) {return false ;}< br> return true;
};
return this;
}

Related Article

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.