Introduction to XMLHttpRequest
The XMLHTTPRequest object is used to exchange data with the server in the background. Is the core object of the Ajax client. A small example:
1 // Initialize the XMLHTTPRequest object 2 VaR Xhr = Null ; 3 VaR Textname = Null ; 4 VaR Infotag = Null ; 5 // Create an XMLHTTPRequest object and return 6 Function Createxmlhttprequest (){ 7 VaR XMLHTTP; 8 If (Window. XMLHttpRequest ){ 9 XMLHTTP = New XMLHttpRequest (); 10 } 11 Else If (Window. activexobject ){ 12 XMLHTTP = New Activexobject ('Microsoft. xmlhttp' ); 13 } 14 Return XMLHTTP; 15 } 16 // Asynchronously check whether the user name exists 17 Function Check (){ 18 Textname = Document. getelementbyid ('user' ); 19 Infotag = Document. getelementbyid ('info' ); 20 VaR Senddata = 'username = '+ Textname. value; 21 If (Textname. value! = ""){ 22 Xhr = Createxmlhttprequest (); 23 Xhr. onreadystatechange = Callback_showdata; 24 Xhr. Open ('post', 'server _ PHP/servercheck. php ', True ); 25 // In post mode, the following two statements are required to set the header: 26 Xhr. setRequestHeader ("Content-Length" , Senddata. Length ); 27 Xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded" ); 28 // Data to be post 29 Xhr. Send (senddata ); 30 } 31 Else { 32 Infotag. innerhtml = 'enter user name' ; 33 } 34 } 35 Function Callback_showdata (){ 36 If (Xhr. readystate = 4 ){ 37 If (Xhr. Status = 200 ){ 38 VaR Data = Xhr. responsetext; 39 // If the user name exists, the user is cleared and the focus is obtained. 40 If (Data. Split ('|') [1] = 'error' ){ 41 Textname. value ='' ; 42 Textname. Focus (); 43 } 44 // If it does not exist, the next input box will be focused. 45 Else { 46 Infotag. Focus (); 47 } 48 // Displays asynchronously transmitted data 49 Infotag. innerhtml = data. Split ('|') [0 ]; 50 } 51 } 52 }
Some methods and attributes:
Method |
Attribute |
Open () |
Onreadystatechange |
SetRequestHeader () |
Status |
Send () |
Readystate |
GetResponseHeader () |
Statustext |
GetAllResponseHeaders () |
Responsetext responsexml responsebody responsestream |
Open
Initialize HTTP Request Parameters, syntax (method, URL, async, username, password ):
Method specifies the request method. Generally, [get, post] is case-insensitive. url specifies the request address, most browsers limit [requires that the URL and request page have the same host name and port] In the same domain, which can be an absolute address or relative address; async specifies whether the request is asynchronous or synchronous, the default value is asynchronous [True]. Generally, you can specify an onreadystatechange handle [used for asynchronous callback], and set synchronization to [false]. the username and password parameters are optional [and must be set when the requested URL requires authorization]; setRequestHeader
Specify an HTTP header separately. syntax (headerstr, valuestr ):
Headerstr specifies the header name and overwrites it if it exists. valuestr specifies the value [This method must be called after the open method]. Send
Send the request to the HTTP server and receive the response. syntax (varbody ):
Specifies the data sent in the request. The synchronous or Asynchronous Method of this method depends on the async parameter in the open method. If async = false, this method will not be returned until the request is completed or times out. If async = true, this method will be returned immediately. GetAllResponseHeaders
Get all HTTP headers of the response, syntax ():
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 (headerstr ):
Headerstr specifies the header name. This method can be called only when the send method is successful. Onreadystatechange
Call the method specified by this attribute when the readystate attribute is changed.
Status
2 **: The operation is successfully received, analyzed, and accepted [200 Common]
4 **: client request Error
5 **: server response error
Statustext
The statustext and status attributes have the same functions. the difference is that status returns an HTTP status code [digit form], while statustext returns HTTP status information in text form. for example, if you request a webpage that does not exist, status will return 404, while statustext will return not found.
Readystate
0 |
Not initialized |
The send () method has not been called. |
1 |
Load |
The send () method has been called and a request is being sent. |
2 |
Load completed |
The send () method is executed successfully and all response content has been received. |
3 |
Interaction |
Parsing response content |
4 |
Complete |
The response content is parsed and can be called on the client |
Responsetext
Responsetext |
Return response information as a string; XMLHTTP sets the encoding of response data as a UTF-8 by default; |
Responsexml |
Format the response information as an XML Document Object and return it. If the response data is not a valid XML document, this attribute does not return xmldomparseerror. You can obtain the error information through the processed domdocument object. |
Responsebody |
Unsigned array format indicates undecoded binary data directly returned from the server |
Responsestream |
Return response information in the form of an ADO Stream Object |
There is also an abort method to cancel the current request.