What do the different readystates in XMLHttpRequest mean, and how can I use them?

JavascriptAjaxXmlhttprequestReadystate

Javascript Problem Overview


XMLHttpRequest has 5 readyStates, and I only use 1 of them (the last one, 4).

What are the others for, and what practical applications can I use them in?

Javascript Solutions


Solution 1 - Javascript

The full list of readyState values is:

State  Description
0      The request is not initialized
1      The request has been set up
2      The request has been sent
3      The request is in process
4      The request is complete

(from https://www.w3schools.com/js/js_ajax_http_response.asp)

In practice you almost never use any of them except for 4.

Some XMLHttpRequest implementations may let you see partially received responses in responseText when readyState==3, but this isn't universally supported and shouldn't be relied upon.

Solution 2 - Javascript

kieron's answer contains w3schools ref. to which nobody rely , bobince's answer gives link , which actually tells native implementation of IE ,

so here is the original documentation quoted to rightly understand what readystate represents :

> The XMLHttpRequest object can be in several states. The readyState attribute must return the current state, which must be one of the following values: > > UNSENT (numeric value 0)
> The object has been constructed. > > OPENED (numeric value 1)
> The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method. > > HEADERS_RECEIVED (numeric value 2)
> All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available. > > LOADING (numeric value 3)
> The response entity body is being received. > > DONE (numeric value 4)
> The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).

Please Read here : W3C Explaination Of ReadyState

Solution 3 - Javascript

Original definitive documentation

0, 1 and 2 only track how many of the necessary methods to make a request you've called so far.

3 tells you that the server's response has started to come in. But when you're using the XMLHttpRequest object from a web page there's almost nothing(*) you can do with that information, since you don't have access to the extended properties that allow you to read the partial data.

readyState 4 is the only one that holds any meaning.

(*: about the only conceivable use I can think of for checking for readyState 3 is that it signals some form of life at the server end, so you could possibly increase the amount of time you wait for a full response when you receive it.)

Solution 4 - Javascript

onreadystatechange Stores a function (or the name of a function) to be called automatically each time the readyState property changes readyState Holds the status of the XMLHttpRequest. Changes from 0 to 4:

0: request not initialized

1: server connection established

2: request received

3: processing request

4: request finished and response is ready

status 200: "OK"

404: Page not found

Solution 5 - Javascript

  • 0 : UNSENT Client has been created. open() not called yet.
  • 1 : OPENED open() has been called.
  • 2 : HEADERS_RECEIVED send() has been called, and headers and status are available.
  • 3 : LOADING Downloading; responseText holds partial data.
  • 4 : DONE The operation is complete.

(From https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMariusView Question on Stackoverflow
Solution 1 - JavascriptKieronView Answer on Stackoverflow
Solution 2 - JavascriptVishal SharmaView Answer on Stackoverflow
Solution 3 - JavascriptbobinceView Answer on Stackoverflow
Solution 4 - JavascriptomertalmiView Answer on Stackoverflow
Solution 5 - JavascriptKhurshid AnsariView Answer on Stackoverflow