First look at the following section of code, and then give you a detailed introduction, Ajax in the readystate (status value) and status (status code), the specific content is as follows:
var getxmlhttprequest = function () {try{//main browser provides XMLHttpRequest object return new XMLHttpRequest ();} catch (E) {///low version of IE browser does not provide XMLHttpRequest object, IE6 below//So must use the specific implementation of IE browser Activexobjectreturn new ActiveXObject (" Microsoft.XMLHTTP ");}; var xhr = Getxmlhttprequest ();//readyState 0=> initialize 1=> load 2=> load complete 3=> parse 4=> finish//Console.log (xhr.readystate ); 0xhr.open ("TYPE", "URL", true);//Console.log (xhr.readystate); 1xhr.send ();//Console.log (xhr.readystate); 1xhr.onreadystatechange = function () {//Console.log (xhr.status);//http Status//Console.log (Xhr.readystate); 2 3 4if ( Xhr.readystate = = = 4 && Xhr.status = = =) {alert (xhr.responsetext);}};
var getxmlhttprequest = function () {if (window. XMLHttpRequest) {return new XMLHttpRequest ();} else if (window. ActiveXObject) {return new ActiveXObject ("Microsoft.XMLHTTP");}; var xhr = Getxmlhttprequest (); Xhr.open ("Get", "1.txt", true); Xhr.send (); Xhr.onreadystatechange = function () {if (xhr.readystate = = = 4) {alert (xhr.responsetext);}};
The service responded with an error, but returned the information, which is not the result we want.
If the return is not 200, but 404 or 500, because only using readystate to judge, it ignores the result of put back is 200, 404 or 500, as long as the response successfully returned, the next JavaScript code, the result will be a variety of unpredictable errors. So only using readystate to judge is not feasible.
Second way of thinking: use status only to judge
var getxmlhttprequest = function () {Try{return new XMLHttpRequest ();} catch (e) {return new ActiveXObject ("Microsoft.XMLHTTP");}}; var xhr = Getxmlhttprequest (), Xhr.open ("Get", "1.txt", true); Xhr.send (); xhr.onreadystatechange = function () {if ( Xhr.status = = =) {alert ("readystate=" + xhr.readystate + Xhr.responsetext);}};
In fact, the results were not as expected. The response code did return 200, but it popped a total of 3 windows! The first is "readystate=2" window, the second is "readystate=3" window, the third time is "readystate=4" window. Thus, it can be seen that the execution of the onreadystatechange function is not only triggered when the readystate becomes 4, but that each change of readystate (2, 3, 4) is triggered, so there is the situation that was said earlier. As you can see, using status alone is not a viable decision.
5. By the above experiment, we can know when to judge ReadyState and status indispensable . Will the order of readystate and status be affected? We can put the status before the first judge, code such as Xhr.status = = = && Xhr.readystate = = = 4
In fact, this has no effect on the final result, but the performance in the middle is different. We know from the experiment that every change of readystate will trigger the onreadystatechange function, if the status is judged first, then each time it will judge the state of one more. Although the performance of the impact is very little, but still should hold the idea of pursuing extreme code, put readystate judgment in front.
Xhr.readystate = = = 4 && xhr.status = = 200
The above is a small part of the discussion about the Ajax in the ReadyState (status value) and status (status code) of the problem, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!
Discuss the problem of readystate (status value) and status (State code) in Ajax