XMLHttpRequest provides the protocol for communication between the client and the http server.
I. Create
IE: http_request = new ActiveXObject ("Msxml2.XMLHTTP ");
Http_request = new ActiveXObject ("Microsoft. XMLHTTP ");
Non-IE: http_request = new XMLHttpRequest ();
Ii. onreadystatechange
Specifies the event handling handle when the readyState attribute changes.
Syntax
Http_request. onreadystatechange = funcMyHandler;
Iii. readyState
Returns the current status of the XMLHTTP request.
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.
4. responseBody
Returns server response data in a certain format.
V. responseStream
Returns a response message in the form of an Ado Stream object.
6. responseText
Returns response information as a string.
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.
7. responseXML format the response information as an Xml Document Object and return
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.
8. status: return the http status code of the current request.
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.
9: xmlhttprequest: statusText Member
StatusText
Returns the response line status of the current request.
10: abort () method
Cancel current request
11: getallresponseheaders () method
Get all http headers of the response
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.
12. getResponseHeader: obtains the specified http header from the response information.
Example:
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.
13: Create an http request using the open () method, specify the method, URL, and authentication information of the request.
Syntax
XMLHttpRequest. open (strMethod, strUrl, blnAsync, strUser, strPassword );
Parameters
StrMethod
Http methods, such as POST, GET, PUT, and PROPFIND. Case Insensitive.
StrUrl
The requested URL address, which can be an absolute or relative address.
BlnAsync [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.
Async is a Boolean value. If it is an asynchronous communication method (true), the client will not wait for the server to respond; if it is a synchronous mode (false), the client will wait until the server returns a message before executing other operations. We need to specify the synchronization mode based on actual needs. In some pages, multiple requests may be sent, or even highly-intensive requests with organized and planned structures, the other will overwrite the previous one. In this case, you must specify the synchronization mode: Flase.
StrUser [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.
StrPassword [Optional]
The password section in the verification information. If the user name is empty, this value is ignored.
Remarks
After calling this method, you can call the send method to send data to the server.
Fourteen: send () method
Send a request to the http server and receive a response
XMLHttpRequest. send (varBody );
Parameters
VarBody
Data to be sent through this request.
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.
15th: setRequestHeader separately specifies an http header of the request
Example:
Copy codeThe Code is as follows:
Var http_request = false;
Function send_request (url)
{// Initialize, specify the processing function, and send the request Function
If (window. XMLHttpRequest) // ILA
{
Http_request = new XMLHttpRequest ();
If (http_request.overrideMimeType)
{
Http_request.overrideMimeType ("text/xml ");
}
}
Else
If (window. ActiveXobject) // IE
{
Try
{
Http_request = new ActiveXObject ("Msxml2.XMLHTTP ");
}
Catch (e)
{
Try {
Http_request = new ActiveXObject ("Microsoft. XMLHTTP ");
}
Catch (e ){}
}
}
If (! Http_request) // An error occurred while creating the object instance.
{
Alert ("You cannot create an XMLHttpRequest instance !! ");
Return false;
}
// Specify the client processing method when the server returns information
Http_request.onreadystatechange = processRequest;
// <SPAN twffan = "done"> determine the request sending method and URL, and whether to execute the following code synchronously.
Http_request.open ("GET", url, true );
Http_request.send (null );
}
//************************************** ****************************
Function processRequest ()
{
If (http_request.readyState = 4) // judge the object status
{
If (http_request.status = 200) // The request result is returned successfully.
{
Alert (http_request.responseBody );
Var a = document. getElementById ("hh"). innerText;
If (a = "1 ")
{
Alert ("unavailable !! ");
}
}
Else // The page is abnormal.
{
Alert ("the page you requested is abnormal ");
}
}
}
//************************************** ******************************
Function userCheck ()
{
Var f = document. Form1;
Var userName = f. username. value;
If (userName = "")
{
Alert ("the user name cannot be blank !! ");
F. username. focus ();
Return false;
}
Else
{
Send_request ("alert. aspx? Username = "+ userName)
}
}