1, Ajax sends the request state to judge if else statement problem.
var xhr = new XMLHttpRequest ();
Xhr.open ("Get", strURL);<br>
<br>xhr.onreadystatechange = function () {
<br><br>
if (xhr.readystate = = 4) {
<br><br><em id= "__mcedel" >
if (xhr.status ===200) {
console.log (Xhr.responsetext);
}
<br><br></em><em style= "line-height:1.5" id= "__mcedel" >
<em id= "__mceDel" > <em id= "__mcedel" ><em id= "__mcedel" >
}else </em></em></em></em><em Style= "line-height:1.5" id= "__mcedel" ><em id= "__mcedel" ><em id= "__mcedel" >
<em id= "__ Mcedel "><em id=" __mcedel ">{<br>
console.log (xhr.status);}
Two 200 status codes were output, then output Xhr.responsetext, and the contents were obtained. It was strange to know how these two 200 came about.
Try to output a lot of content at that time, but still can not find the reason. Only to change the Else statement to Console.log (xhr.readystate) only to find the problem, then output node is 2, 3 in turn, the content obtained. Only then found the reason:
XHR send a request to the end of the request, will pass through 5 states, the status code is 0,1,2,3,4 (that is, readystate value), every time the state changes, will trigger the ReadyStateChange event, invoke the event handler function. When the status code is changed to 2 o'clock, it calls the ReadyStateChange event and then walks the If/else statement,
This is readystate obviously not 4, then walk else statement output 2. And so on, 3,4 call the ReadyStateChange event, only 4 o'clock to go if the contents of the, Request success, return the request content.
The above is a small set to introduce the Ajax error handling methods, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!