Details about the functions of async: false/true in Ajax requests (ajax is called externally) and ajaxasync

Source: Internet
Author: User

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.

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.