Async: false and async: true in Ajax requests, ajaxasync

Source: Internet
Author: User

Async: false and async: true in Ajax requests, ajaxasync

Example:

function test(){  var temp="00";  $.ajax({    async: false,    type : "GET",    url : 'userL_checkPhone.do',    complete: function(msg){      alert('complete');    },    success : function(data) {      alert('success');      temp=data;      temp="aa";    }  });  alert(temp);  }

CheckPhone () method in UserLAction

  public void checkPhone() throws IOException {    this.getServletResponse().setContentType("text/html; charset=UTF-8");    this.getServletResponse().setHeader("Cache-Control", "no-cache");    PrintWriter out = this.getServletResponse().getWriter();    out.print("true");  }

Async: false (true by default );

When async: false is synchronous, the Ajax request in the test () method will lock the entire browser,

Other operations can be performed only after userL_checkPhone.do is executed.

Therefore, the execution result is first alert ('success'); alert ('complete'); alert ("aa ");

When async: true, ajax requests are asynchronous. However, there is a problem: the ajax request in test () and the subsequent operations are executed asynchronously. When userL_checkPhone.do is not completed, the operation following the ajax request may have been executed,

Therefore, the result is alert ('success'); alert ('complete'); alert ("00 ");

In this way, we will find that alert ("success") and alert (temp) are almost synchronous, so temp is the initial value temp = "00", rather than temp = "aa ";

The differences between async: false and async: true in the preceding Ajax request are all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's house.

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.