XMLHttpRequest Object Properties and Events
The XMLHttpRequest object exposes various properties, methods, and events so script can process and control HTTP requests and responses. The rest of this article discusses these in detail.
The readyState Property
The XMLHttpRequest object cycles through several states as it sends an HTTP request to the server, waits while the request is processed, and when it receives a response. So that scripts can respond appropriately to the various states, the object exposes a
readyState property that represents the object's current state, as shown in Table 1.
Table 1. The XMLHttpRequest Object's ReadyState Property Values: The table shows the possible values of the readyState property and an explanation of each.
| ReadyState Value |
Description |
| 0 |
Represents an "uninitialized" state in which an XMLHttpRequest object has been created, but not initialized. |
| 1 |
Represents a "sent" state in which code has called the XMLHttpRequest open() method and the XMLHttpRequest is ready to send a request to the server. |
| 2 |
Represents a "sent" state in which a request has been sent to the server with the send() method, but a response has not yet been received. |
| 3 |
Represents a "receiving" state in which the HTTP response headers have been received, but message body has not yet been completely received. |
| 4 |
Represents a "loaded" state in which the response has been completely received. |
The onreadystatechange Property
The XMLHttpRequest object fires a
readystatechange event whenever the
readyState value changes. The
onreadystatechange property accepts an EventListener value, specifying the method that the object will invoke whenever the
readyState value changes.
The responseText Property
The
responseText property contains the text of the HTTP response received by the client. When the
readyState value is
0,
1, or
2 responseText contains an empty string. When the
readyState value is
3 (Receiving), the response contains the incomplete response received by the client. When
readyState is 4 (Loaded) the
responseText contains the complete response.
The responseXML Property
The
responseXML property represents the XML response when the complete HTTP response has been received (when
readyState is
4), when the Content-Type header specifies the MIME (media) type as
text/xml,
application/xml, or ends in
+xml. If the Content-Type header does not contain one of these media types, the
responseXML value is
null. The
responseXML value is also
null whenever the
readyState value contains any value other than
4.
The
responseXML property value is an object of type Document interface, and represents the parsed document. If the document cannot be parsed (for example if the document is malformed or the character encoding of the document is not supported) the
responseXML value is
null.
The status Property
The status property represents the HTTP status code and is of type short. The status attribute is available only when the readyState value is 3 (Receiving) or 4 (Loaded). Attempting to access the status value when readyState is less than 3 raises an exception.
The statusText Property
The statusText attribute represents the HTTP status code text and is also available only when the readyState value is 3 or 4. Attempting to access the statusText property for other readyState values raises an exception.