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

5

114

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?

Aixenprovence answered 11/3, 2009 at 0:3 Comment(0)
G
174

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.

Gruver answered 11/3, 2009 at 0:14 Comment(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.Barmy
+1 to @MattBianco for his advice. I would also like to add that the Mozilla Developer Network MDN 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.Negrito
@GregPettit , can we use readystate 0 , for no internet connection , or there are any reasons do exists where "The request is not initialized"Luker
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.Barmy
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.Hypoplasia
L
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

Luker answered 17/4, 2014 at 9:28 Comment(2)
@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...Luker
Huh, I just tried that out in the sandbox and it seemed to work.Sundstrom
I
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.)

Iaea answered 11/3, 2009 at 0:15 Comment(0)
R
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

Refractory answered 6/8, 2015 at 10:27 Comment(0)
C
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)

Claytor answered 9/1, 2018 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.