113

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?

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Marius
  • 54,363
  • 28
  • 121
  • 143

5 Answers5

172

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.

Colin
  • 1,863
  • 2
  • 16
  • 20
Kieron
  • 10,832
  • 5
  • 31
  • 28
  • 5
    Errors in the transfer still update readyState to 4. However, a completely interrupted transfer will reset readyState to 0. So, an error in the transfer that's a result of a redirect isn't really an "error" and you can choose to ignore logging/reporting the error if the readyState of the xhr is 0. It's a bit fragile, and your mileage may vary depending on whether logging every event/error is a "must have" or a "nice to have". If it's the latter, you can afford to lose the rare event that may be an error along with readyState of 0. – Greg Pettit May 29 '13 at 19:12
  • 2
    +1 to @MattBianco for his advice. I would also like to add that the Mozilla Developer Network [MDN](https://developer.mozilla.org/) is arguably one of the most popular and trustworthy reference sites out there for all your JavaScript/HTML/CSS needs. When doing a google search, prepend your query with "mdn" and you'll save yourself some headaches. – DondeEstaMiCulo Feb 26 '14 at 22:15
  • @GregPettit , can we use readystate 0 , for no internet connection , or there are any reasons do exists where "The request is not initialized" – Vishal Sharma Apr 17 '14 at 09:09
  • 4
    I'm not sure, @vishalsharma -- as I recall, the readystate is 0 as long as the request is not initialized. After that, any other "completion" (either by success or error) will update the readystate to 4. So losing internet after the transfer starts will flip it to 4, not back to 0. If there has never been internet when the transfer is attempted, it should still be at 0; it will only move to 1 after handshake. – Greg Pettit Apr 17 '14 at 14:28
  • The explanation for readyState 2 is wrong – or at least completely misleading. Shure the request was sent, but those state actually says that all final response headers were received. – inta Oct 02 '17 at 15:43
32

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

Community
  • 1
  • 1
Vishal Sharma
  • 2,561
  • 2
  • 20
  • 34
  • @CharlesWood , challenge for me was here stackoverflow markdown editor won't accept refer to w3c.. check out hence i put short url from goo.gl.... i don't understand why it won't allow me to directly put w3c url... – Vishal Sharma Apr 25 '14 at 06:47
  • Huh, I just tried that out in the [sandbox](http://meta.stackexchange.com/questions/3122/formatting-sandbox/) and it seemed to work. – Charles Wood Apr 25 '14 at 15:39
22

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.)

BenMorel
  • 30,280
  • 40
  • 163
  • 285
bobince
  • 498,320
  • 101
  • 621
  • 807
1

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

omertalmi
  • 98
  • 2
  • 10
0
  • 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)

Khurshid Ansari
  • 3,258
  • 2
  • 24
  • 42