Error handling methods _ajax related to Ajax mistakes

Source: Internet
Author: User
Tags error handling

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.