This time I will bring you what are the ways of Ajax request async? How to use it, what are the precautions for Ajax request async, the following is a practical case, let's take a look.
test.html code:
<a href="javascript:void(0)" onmouseover="testAsync()">
asy.js code:
function testAsync(){ var temp; $.ajax({ async: false, type : "GET", url : 'tet.php', complete: function(msg){ alert('complete'); }, success : function(data) { alert('success'); temp=data; } }); alert(temp+' end'); }
tet.php code :
<?php echo "here is html code"; sleep(5); ?>
async: false, (default is true);
As above: false means synchronization. The Ajax request in this testAsync() method locks the entire browser. Only after the execution of tet.php is completed, Only then can you perform other operations.
When async: true, the ajax request is asynchronous. But there is a problem: the ajax request in testAsync() and the subsequent operations are executed asynchronously, so when tet.php has not been executed, the operations following the ajax request may have been executed.
For example: alert(temp+' end');
However, the temp data is assigned after the ajax request success, and as a result, it will be empty when output.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Methods for ajax to read Json data
Methods to construct AJAX to implement form JSON conversion
The above is the detailed content of What are the ways to request async with Ajax? how to use. For more information, please follow other related articles on the PHP Chinese website!