Notes for asynchronous and synchronous Ajax requests

Source: Internet
Author: User

When I was working on a project, I used a table control and its data was asynchronously loaded. I used some business processing in the function of Binding data to this table. When I ran the page, I used a browser to place a breakpoint tracking on the place where I wrote business processing. It was actually executed. when it could be finally displayed, its processing was overwritten. I was wondering, at that time, I also knew that the table binding function had a problem, but it was time-consuming. Its functions were encapsulated and not tracked in detail, this is a clumsy method, but it is also an effective method.

Later, I checked the document and found some information. In fact, the principle should be as follows:

By default, all requests are asynchronous requests. To send a synchronization request, set this option to false. Note: The synchronous request locks the browser. Other operations can be performed only after the request is completed.Copy codeThe Code is as follows: var temp;
$. Ajax ({
Async: false,
Type: "POST ",
Url: defaultPostData. url,
DataType: 'json ',
Success: function (data ){
Temp = data;
}
});
Alert (temp );

This ajax request is a synchronous request. alert (temp) will not execute until no return value is returned.
If async is set to true, the ajax statement is executed directly without waiting for the results returned by the ajax request.

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.