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