Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

readyState vs status==200

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?

like image 615
user2507818 Avatar asked Jul 10 '13 02:07

user2507818


People also ask

What is if this readyState == 4 && this 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.

What is the meaning of readyState == 4 in Ajax?

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 does readyState 3 mean?

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.


2 Answers

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.

like image 55
Ray Nicholus Avatar answered Sep 27 '22 22:09

Ray Nicholus


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!'

like image 35
Aguardientico Avatar answered Sep 27 '22 22:09

Aguardientico