As Ajax is just getting used to it, the XMLHTTPRequest object has never been heard of it. Before getting started, let's take a look at its attributes and methods to pave the way for its use. This article focuses on the attributes and methods of XMLHttpRequest.
Attributes and events of the XMLHTTPRequest object
Attribute |
Description |
Readystate |
Status of the XMLHTTPRequest object [1] |
Responsetext |
Contains the HTTP text content received by the Client [2] |
Responsexml |
DOM object corresponding to the XML content returned by the server [3] |
Status |
HTTP status code returned by the server [4] |
Statustext |
Text Information of the server return status code [5] |
Event |
Description |
Onreadystatechange |
This event is triggered when the readystate attribute changes and is used to trigger the callback function. |
[1]:
Status |
Name |
Description |
0 |
Uninitialized |
Initialization status. The XMLHTTPRequest object has been created or reset by the abort () method. |
1 |
Open |
The open () method has been called, but the send () method has not been called. The request has not been sent. |
2 |
Send |
The send () method has been called and the HTTP request has been sent to the web server. No response is received. |
3 |
Processing ing |
All Response Headers have been received. The response body starts receiving but is not completed. |
4 |
Loaded |
The HTTP response has been fully received. |
[2]: When readystate = 4, responsetext contains the complete response information.
When readystate = 3, responsetext contains incomplete response information.
When readystate is <3, responsetext is an empty string.
[3]: When readystate is 4 and the MIME type of Content-Type in the response header is XML (text/XML or application/XML, this property has a value and is parsed into an XML document. In other cases, null is returned, including bad XML documents that are returned or incomplete responses.
[4]: for example, 200 indicates successful, and 404 indicates "notfound" error. When readystate is less than 3, reading this attribute will cause an exception.
[5]: When the status is 200, it is "OK", and when the status is 404, it is "not found ". Like the status attribute, when readystate is less than 3, reading this attribute causes an exception.
XMLHTTPRequest object Method
1. Open Method
Description: The HTTP method, URL address, and other request information used to interact with the server.
Open (method, URL, async, username, password) is used for initialization.
Return Value: Get an object containing the send () method
Method: required. It is used to specify the HTTP Request Method and supports all HTTP methods, such as get and post, as specified
Uri: the address of the requested server, which is automatically resolved to an absolute address.
Async: whether the request is asynchronous. "True" indicates that the request is asynchronous. "false" indicates that the request is synchronous. The default value is "true.
Username, password: it can be left unspecified, indicating the user name and password respectively, providing the user name and password required by the HTTP authentication mechanism.
After open is called, The readystate status is 1.
2. Send (content) Method
Description: a request sent to the server. Its content can be a DOM object, input stream, or string.
After calling the open method, you can call the send () method to send the request.
When async = true in open, the send () method is returned immediately after it is called. Otherwise, the request is interrupted until it is returned.
3. Abort () method
This method can suspend receiving of an httprequest request or httpresponse, and set the XMLHttpRequest status to initialization.
4. setRequestHeader (header, value) Method
This method is used to set the request header information. This method must be called after the open method.
5. getResponseHeader () method
Description: return information about all HTTP Response Headers, including Content-Length, date, and Uri.
When readystate is greater than 2, this method is used to retrieve the response header information. Otherwise, an empty string is returned.
The getAllResponseHeaders () method returns all httpresponse header information.
After learning about the XMLHTTPRequest object, the focus is on how to use it. Please pay attention to my next blog "ajax entry-use XMLHTTPRequest object in five steps".