Details about the functions of async: false/true in Ajax requests (ajax is called externally) and ajaxasync
Test.html
<A href = "javascript: void (0)" rel = "external nofollow" onmouseover = "testAsync ()"> asy. jsfunction testAsync () {var temp; $. ajax ({async: false, // synchronous request type: "GET", url: 'tet. php ', complete: function (msg) {alert ('complete') ;}, success: function (data) {alert ('success '); temp = data ;}}); alert (temp );}
Tet. php
<?php echo "here is html code"; sleep(5);?>
Description
Async: false (true by default );
As shown above: false indicates synchronization. The Ajax request in the testAsync () method will lock the entire browser. Other operations can be performed only after the execution of tet. php is complete.
When async: true, ajax requests are asynchronous.
However, there is a problem: ajax requests in testAsync () and subsequent operations are executed asynchronously, so when tet. php may have executed the following operations after the ajax request, such as alert (temp + 'end'). However, the temp data is assigned a value after the ajax request success. The result is null in the output.
----------------------------------------------------
Because ajax is called asynchronously by default, ajax return values are rarely obtained directly. But sometimes because the business needs must be obtained through ajax, I provide two solutions here.
Specific Method:
1. Just like passing throughSet async to false for asynchronous synchronization;
2. You can run the following parameters in success.
You can select one of the two solutions based on your project.
When using the first method, you can choose not to have too many performance requirements.
The above detailed discussion about the role of async: false/true in Ajax requests (ajax in external calls) is all the content that I have shared with you, and I hope to give you a reference, we also hope that you can support the customer's home.