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