How to Use XMLHTTP

Source: Internet
Author: User
The XMLHTTP object is an HTTP-based, XML-based Web Resource object included in Microsoft's MSXML development kit. it appears from msxml3.0. it is mainly used in Ajax technology to obtain information from other network resources, and then Javascript is used to update some content in the page. you can use this method to refine the content that can be updated on the page. You do not need to update a very small amount of content to refresh the entire page. the usage of XMLHTTP objects is as follows: 1-creating XMLHTTP objects. Different browsers have different creation methods. This article uses ie as an example to describe how to create XMLHTTP objects. 2-Use an XMLHTTP object to send request information to external resources and obtain the returned results synchronously or asynchronously 3-process the returned results, which is displayed on the page, the following describes how to use the JavaScript-related technology XMLHTTP object: (the function prototype uses the VB syntax) Sub abort ()
Interrupts the HTTP request of the current object.
 

Function getAllResponseHeaders () as string

Obtains all header information in the HTTP Response Results, expressed in string format.

 

 Function getResponseHeader (bstrheader as string) as string

 
Obtains the value of the specified name in the HTTP Response Header, which is represented by a string. If no name exists, an empty string is returned.
Sub open (bstrmethod as string, bstrurl as string, [varasync], [bstruser], [bstrpassword])
Initialize the HTTP connection request object, set the request method, address, and authentication information. available bstrmethod values include get, post, and head, which define the method for submitting requests to HTTP. bstrurl is required as the HTTP resource address to be submitted. varasync is required, set whether to return results asynchronously or synchronously. The options are true-asynchronous and false-synchronous. The default options are asynchronous. If the HTTP request requires username and password, the options are bstruser and bstrpassword.
Sub send ([varbody])
Send an HTTP request to the server and obtain the returned result. varbody is the data to be sent to the server, which is usually used in post mode.
Sub setRequestHeader (bstrheader as string, bstrvalue as string)
Set bstrheader/bstrvalue pairs in the request header and send them to the server. For example:

Xmlreq. setRequestHeader ("Content-Type ",

"Application/X-WWW-form-urlencoded; charset = UTF-8 ");

 
 

XMLHTTP attributes

 

Onreadystatechange

 
Set the function object to be called when the readystate of the request object changes;

Readystate

 

The status value of the request object, for example:

0-the request object is created but not initialized. That is, the open method is not called.

1-loading, the open method has been called, and the send method has not been called

2-loaded, the send method has been called, but the header information has not been obtained

3-some information has been obtained during interaction. When responsetext is called, incomplete information is obtained and an error is returned.

4-all data has been received. You can use responsetext or responsebody to obtain the complete data.

 

Responsebody

 

Indicates the returned original information from the HTTP response, the encoding of the content depends on the request server side (UTF-8, UCS-2, UCS-4, shift_jis, etc)

 

Responsetext

 

The string representation of the Data body returned by the HTTP request. It is returned after UTF-8 encoding by default. If the returned content contains Chinese characters, the data on the server must be UTF-8 encoded; otherwise, garbled characters will occur.

 

Responsestream

 

The stream object of the data returned by the HTTP request. This object implements the istream interface.

 

Responsexml

 

Returns the data object in XML format. the data returned by the server is in XML format. when the server generates XML in Dynamic Language, the Content-Type must be set to text/XML; otherwise, the responsexml obtained by the client is empty.

 

Status

HTTP return code. 200-success 404-error request 500-internal server error, etc. For details, see http.
Statustext
Description of HTTP return status text.
The methods and attributes of XMLHTTP are described above. Some examples are listed below. example 1 connect to Google and display the obtained information <script language = "JavaScript"> function getgoogle () {var xmlreq; try {xmlreq = new activexobject ("Microsoft. XMLHTTP "); var web =" http://www.google.com "; // asynchronous xmlreq. open ("get", web, true); xmlreq. onreadystatechange = function () {If (xmlreq. readystate = 4) {document. write (xmlreq. responsetext) ;}} xmlreq. send (); catch (e) {alert (e) ;}</SCRIPT> Example 2 use the POST method to send data to webserver <script language = "JavaScript"> var xmlreq; function senddata () {try {xmlreq = new activexobject ("msxml2.xmlhttp"); var DATA = "name = James & id = 1234 "; vaR web = "http://www.myweb.com/login.jsp" // the actual execution should be replaced with an existing address xmlreq. open ("Post", web, true); xmlreq. onreadystatechange = reshandler; // set the xmlreq. setRequestHeader ("Content-Type ","

Xmlreq. setRequestHeader ("Content-Type ",

"Application/X-WWW-form-urlencoded; charset = UTF-8 ");

Xmlreq. send (data) ;}catch (e) {alert (e) ;}} function reshandler () {If (xmlreq. readystate = 4) {alert (xmlreq. responsetext) ;}}</SCRIPT> In the Mozilla browser, use xmlreq = new XMLHttpRequest () to create an HTTP request object. other usage is the same as MSXML. Based on security questions, assuming that the region in open is another domain, the browser generally disallows such operations. You need to modify the Security Configuration for cross-origin region requests. Exam documents: 1-http://msdn.microsoft.com in MSXML sdk2-http://jibbering.com/2002/4/httprequest.html

How to Use XMLHTTP

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.