The difference between an AJAX synchronous request and an asynchronous request

Source: Internet
Author: User

Ajax differences:

Async: Boolean value that indicates whether the request is asynchronous mode. Async is important because it is used to control how JavaScript executes the request.

When set to true, the request will be sent in asynchronous mode, the JavaScript code will continue to execute without waiting for a response, and an event handler must be used to monitor the response of the request.

If Async is set to False, the request will be sent in synchronous mode, and JavaScript will wait until the response is received before continuing to execute the remaining code.

This means that if the response time is long, the user will not be able to interact with the browser until it receives a response.

For this reason, the best practice for AJAX application development is to use asynchronous requests to achieve data acquisition and to use synchronous requests to send and receive simple messages between servers.

This is useful for scenarios where you want the user to wait for a response, or if you want to receive very little data (for example, less than 1KB). For the usual amount of data or large amounts of data, it is best to use asynchronous calls.

For example, the following example shows an incorrect return result because the Ajax asynchronous request has not been completed and the function has already executed the return.

1 Asynchronous Request mode:

$.ajax ({     URL: ' Your URL ',     data:{name:value},     Cache:false,     async:true,     type: ' POST ',     DataType: ' json/xml/html ',     success:function (result) {do         something ....     

2 Synchronous Request Mode

$.ajax ({     URL: ' Your URL ',     data:{name:value},     Cache:false,     async:false,     type: ' POST ',     dataType: ' json/xml/html ',     success:function (result) {do         something ...     }});         

  

The difference between an AJAX synchronous request and an asynchronous request

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.