xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("myDiv").innerHTML = xmlhttp.responseText; } }
Above code is from:http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp.
Question:
According to this tutorial:
readyState: 4: request finished and response is ready status: 200: "OK" When readyState is 4 and status is 200, the response is ready:
since when xmlhttp.readyState == 4
, response is ready, why do we still need xmlhttp.status == 200
? what is the difference between xmlhttp.readyState == 4
and xmlhttp.status == 200
?
readyState: 4: request finished and response is ready status: 200: "OK" When readyState is 4 and status is 200, the response is ready: since when xmlhttp. readyState == 4 , response is ready, why do we still need xmlhttp.
State 4 means that the request had been sent, the server had finished returning the response and the browser had finished downloading the response content. So, it is right to say that the AJAX call has completed.
What is XHR readyState=3 ? Having the readyState with a value of 3 it means that the current state is LOADING . So when is a readyStateChange event for that state fired? Whenever an asynchrounous request does switch to the LOADING state.
The status of the response, xhr.status
, is (generally) used to determine whether the request was successful or not. xhr.readyState
is simply used to determine the state of the request, such as "has not yet been sent" (0), "complete and response received" (4), etc.
The server is responsible for providing the status
, while the user agent provides the readyState
.
status indicates if server response is ok.
In general words, when you got an status
500 - 599: the server had an error
400 - 499: this is a client error (Ex: 404 page not found)
300 - 399: then exists a redirect
200 - 299: then it is correct and
100 - 199: means information message
Then the status==200 is getting you a message where the server says: 'Hey man I do the work!'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With