XMLHttpRequest asynchronous and synchronous Modes

Source: Internet
Author: User

I. Differences between synchronization and Asynchronization

To put it simply, synchronization is the blocking mode, while Asynchronous is the non-blocking mode.

For example: Normal B/S mode (synchronous) Ajax technology (asynchronous)
Synchronization: submit a request, wait for the server to process, and return the result. The client browser cannot do anything during this period.
Asynchronous: requests are triggered through events-> server processing (this is what the browser can do)-> processing is complete

Ii. XMLHttpRequest asynchronous and synchronous Modes

XMLHTTP. Open ("get", URL, false); // requests XMLHTTP. Open ("get", URL, true) in synchronous mode // asynchronous

Iii. Project Example

XMLHttpRequest is sometimes used in projects, but when synchronization is used and when Asynchronization is used. Sometimes the gap cannot be seen, but sometimes an error occurs and you do not know why, sometimes it is correct, sometimes it is wrong to execute.

To use XMLHttpRequest to upload files for testing, we need to first upload the files to the server, then save the form information and save the URL of the uploaded file.

The following is the correct code and the synchronization method is used.

VaR FD = new formdata (); FD. append ("filetoupload", top.20.20.doc ument. getelementbyid ('filetoupload '). files [0]); var xhr = new XMLHttpRequest (); xhr. open ("Post", Web + "/thesis/importpdf", false); // upload the file to the server xhr. send (FD); xhr. addeventlistener ("LOAD", uploadcomplete (DIALOG), false); // After the upload is complete, save the form information and upload file path information.

The above code needs to be executed in two places: xhr. open ("Post", Web + "/thesis/importpdf", false); and xhr. addeventlistener ("LOAD", uploadcomplete (DIALOG), false );

If we use xhr. open ("Post", Web + "/thesis/importpdf", true); // asynchronous mode. If the file is small, the path information may be saved in step 2 as usual, however, the file size is too large and it takes time to upload it. If it is asynchronous, the first step of File Upload is not complete, and the second step is executed, but we cannot get the file upload path information. Therefore, a blocking synchronization method is required.

 

XMLHttpRequest asynchronous and synchronous Modes

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.