Break down the differences between async:false and async:true in Ajax requests

亚连
Release: 2018-05-22 17:23:25
Original
1606 people have browsed it

Below I will bring you an article detailing the differences between async:false and async:true in Ajax requests. Let me share it with you now and give it as a reference for everyone.

The example is as follows:

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); }
Copy after login

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"); }
Copy after login

async: false, (default is true);

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

Only userL_checkPhone Only after the execution of .do is completed, other operations can be performed.

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

When async: true, the ajax request is asynchronous. But there is a problem: the ajax request in test() and the subsequent operations are executed asynchronously, so when userL_checkPhone.do has not yet been executed, the operations following the ajax request may have been executed,

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

In this way, you will find that alert("success") and alert(temp) are executed almost simultaneously. So temp is the initialized value temp = "00", not temp="aa";

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Ajax and mysql data interaction to create a message board function

Ajax synchronization and asynchronous problem analysis and solutions

Instances of ajax responding to json strings and json arrays (graphic tutorial)

The above is the detailed content of Break down the differences between async:false and async:true in Ajax requests. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!