readyState
Summary
Returns the current state of the XMLHttpRequest.
Property of apis/xhr/XMLHttpRequestapis/xhr/XMLHttpRequest
Syntax
Note: This property is read-only.
var state = xhr.readyState;
Return Value
Returns an object of type unsigned shortunsigned short
Returns one of the following values:
- UNSENT (0): open() has not been called yet.
- OPENED (1): send() has not been called yet.
- HEADERS_RECEIVED (2): send() has been called, and headers and status are available.
- LOADING (3): Downloading; responseText holds partial data.
- DONE (4): The operation is complete.
Examples
Prints a message to the console log during each state of the request.
function handler() { if (xhr.readyState === xhr.UNSENT) { console.log('XHR Unsent'); } else if (xhr.readyState === xhr.OPENED) { console.log('XHR Opened'); } else if (xhr.readyState === xhr.HEADERS_RECEIVED) { console.log('XHR Headers received'); } else if (xhr.readyState === xhr.LOADING) { console.log('XHR Loading'); } else if (xhr.readyState === xhr.DONE) { console.log('XHR Done'); } } var xhr = new XMLHttpRequest(); xhr.open("GET", "http://localhost/test.xml", true); xhr.onreadystatechange = handler; xhr.send();
Notes
You cannot call the responseText property to obtain partial results (readyState = 3). Doing so will return an error, because the response is not fully received. You must wait until all data has been received. See onreadystatechange.
Related specifications
- W3C XMLHttpRequest Specification
- W3C Working Draft
Attributions
Microsoft Developer Network: Windows Internet Explorer API reference Article