XMLHttpRequest
has 5 readyState
s, 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?
XMLHttpRequest
has 5 readyState
s, 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?
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 :
Please Read here : W3C Explaination Of ReadyState
Original definitive documentation
0
,1
and2
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 theXMLHttpRequest
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.)(From https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState)
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
The full list of
readyState
values is:(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
whenreadyState==3
, but this isn't universally supported and shouldn't be relied upon.