The XMLHTTP object is an object in Microsoft's MSXML development package that uses the Http,xml protocol to access Web resources. From the beginning of the MSXML3.0. It is used primarily in AJAX technology to obtain information from other network resources and then by JavaScript to update parts of the page. This way, the page can be updated to refine the content without having to update very little content to refresh the entire page. The use of XMLHTTP objects such as the following: 1-Create a XMLHTTP object, different browser creation method, this article takes ie as an example. 2-Use XMLHTTP object to send request information to external resource, synchronously or asynchronously to get return result 3-process The returned result, display on the page, then use the method of JavaScript related technique XMLHTTP object such as the following: (function prototype adopts VB syntax)
Sub abort ()
interrupts the HTTP request of the current object.
Function getallresponseheaders () as String
Gets all header information in the HTTP response result, expressed in string format
Function getResponseHeader (Bstrheader As String) as String
gets the value of the specified name in the header of the HTTP response, expressed as a string, if not present, returns an empty string
Sub Open (Bstrmethod As String, bstrURL as String, [Varasync], [Bstruser], [Bstrpassword])
initializes the HTTP connection request object, sets the request method, address, authentication information. Bstrmethod the available values are Get,post,head, which defines how the request is submitted to HTTP; Required bstrURL is the address of the HTTP resource to be interviewed; required Varasync Optionally, whether the setting is asynchronous or synchronous waits for the return result, true-asynchronous, false-synchronous, default is asynchronous, assuming that the HTTP request requires username and password, is set in Bstruser,bstrpassword.
Sub Send ([varbody])
sends an HTTP request to the server and gets the result returned. Varbody is the data to be sent to the server and is typically used in post mode.
Sub setRequestHeader (Bstrheader As String, Bstrvalue as String)
Set the Bstrheader/bstrvalue value pair in the request header and send it to the server side. Example:
Xmlreq.setrequestheader ("Content-type",
"Application/x-www-form-urlencoded; Charset=utf-8 ");
XMLHTTP Property
onReadyStateChange
sets the function object to invoke when the Request object state readystate changed;
ReadyState
The state value of the request object, meaning such as the following:
0-The Request object was created but not initialized, that is, the open method did not call
1-load, the open method has been called, the Send method is not called
2-loaded, the Send method has been called, but the header information has not been obtained
3-In the interaction, some information has been obtained, then call ResponseText will get incomplete information, will return an error
4-All data has been received, complete data can be obtained with responsetext or responsebody
Responsebody
Represents the return of the original information from the HTTP response, the encoding of the content depends on the server side of the request (UTF-8, UCS-2, UCS-4, Shift_JIS, etc.)
ResponseText
The HTTP request returns the string representation of the data body, which is returned by default with Utf-8 encoding, assuming that there is Chinese in the returned content, the server side of the data must be encoded with utf-8, otherwise garbled.
Responsestream
The HTTP request returns the stream object of the data that implements the IStream interface.
Responsexml
Returns an XML-formatted data object. The server side returns data in XML format when it is available. server side when generating XML in dynamic language, you must set Content-type to Text/xml, otherwise the responsexml of the client will be empty
Status
http return code. 200-Success 404-Error request 500-server internal error, and so on. See the HTTP protocol.
StatusText
http return status text descriptive narrative.
The above describes the methods and properties of XMLHTTP, using some of the following example . examples 1 connect to Google, display the information obtained <script language= " JavaScript "> function getgoogle () { var xmlreq; try{ xmlreq = new ActiveXObject ("Microsoft.XMLHTTP"); var web = "http://www.google.com"; //Async mode 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 sending data to Webserver <script language= using the Post method " 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 " //actual execution to be replaced by an address that exists Xmlreq.open ("POST", Web,true); xmlreq.onreadystatechange = reshandler; //Set return value handler function 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 Mozilla browser, use XMLreq = new XMLHttpRequest () to create an HTTP request object. Other usages are the same as MSXML. Based on security issues, let's say that access to open is in another domain, and browsers generally prohibit such operations, and cross-domain access needs to change securityReset references in document: 1-http://msdn.microsoft.com MSXML sdk2-http://jibbering.com/2002/4/ httprequest.html
XMLHTTP Use specific explanations