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.