Ajax Asynchronous & Synchronous requests

Source: Internet
Author: User

First, IntroductionThe most important issue with AJAX requests is the order in which the code executes. The longest problem is that we define a variable to receive the return result of the Ajax asynchronous request, followed by the code, but the subsequent code is used when the variable is the initial value, always get the desired result!!!
Ii. Examples
Sync var  email = "[email protected]"; Console.log (1); Jquery.ajax ({url: "/invite/sendemailajax.pt", type: "Post", DataType: "Text", Async:true,data: "inviteemails=" +email,success:function (data) {Console.log (2);}}); Console.log (3);//Result: 1->3->2

asynchronous var  email = "[email protected]"; Console.log (1); Jquery.ajax ({url: "/invite/sendemailajax.pt", type: "Post", DataType: "Text", Async:false,data: "inviteemails=" +email,success:function (data) {Console.log (2);}}); Console.log (3);//Result: 1->2->3
Iii. Official Explanationsby default, all requests be sent asynchronously (i.e. this is set to True by default). IF you need synchronous requests, set this option to false. Cross-domain requests and DataType: "JSONP" requestsdon't support synchronous operation. Note that synchronous requests could temporarily lock the browser, disabling any actions and the request is active.

Sync. The default is true, which is the asynchronous way, $. After Ajax executes, it continues executing the script behind Ajax until the server-side returns data, triggering $. The success method in Ajax, which executes two threads at this time. To set it to false, all requests are synchronous requests, and the synchronization request locks the browser before the return value, and the user's other actions must wait for the request to complete before it can be executed.
iv. Reference URLhttp://api.jquery.com/jQuery.ajax/

Ajax Asynchronous & Synchronous requests

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.