Differences between Ajax synchronization and Asynchronization

Source: Internet
Author: User

 

VaR returnvalue = NULL;
XMLHTTP = createxmlhttp ();
XMLHTTP. onreadystatechange = function (){
If (XMLHTTP. readystate = 4 & XMLHTTP. Status = 200 ){
If (XMLHTTP. responsetext = "true "){
Returnvalue = "true ";
}
Else {
Returnvalue = "false ";
}
}
};
XMLHTTP. Open ("Post", URL, true); // asynchronous transmission
XMLHTTP. setRequestHeader ("If-modified-since", "0"); // do not cache Ajax
XMLHTTP. Send (sendstr );
Return returnvalue;

 

 

 

 

 

 

The xmlhttpreq. onreadystatechange status value can be used only in asynchronous mode! The following are different asynchronous and synchronous call methods:
Java code

Xmlhttpreq. Open ("get", URL, true); // asynchronous

Xmlhttpreq. onreadystatechange = showresult; // showresult is the name of the callback function.
Xmlhttpreq. Send (null );

Function showresult (){
If (xmlhttpreq. readystate = 4 ){
If (xmlhttpreq. Status = 200 ){
******
}
}
}

Java code
Xmlhttpreq. Open ("get", URL, false); // synchronous Mode
Xmlhttpreq. Send (null );
Showresult (); // although showresult is a callback function name, its usage is different ~

Function showresult (){
// If (xmlhttpreq. readystate = 4) {You don't need to use it here. Directly dosomething ~
// If (xmlhttpreq. Status = 200 ){
* ***** // Dosomething
//}
//}
}

 

 

XMLHTTP. Open ("Post", URL, true );
If it is synchronous (false), the return value is true or false. Because onreadystatechange is executed after sending, the program waits until onreadystatechange is executed, after responsetext is obtained, the next statement is executed. Therefore, returnvalue must have a value.
If it is asynchronous (true), the return value must be null, because the program continues to execute the next statement after sending does not wait for the XMLHTTP response, therefore, null is returned if returnvalue is not changed.

Http://te343.w3.sh.cn/blog/view.aspx? Blogid = 82

 

If you want to obtain the XMLHTTP return value, you must use synchronization. the return value cannot be obtained asynchronously.

When using the XMLHTTP pool synchronously and asynchronously, note that only XMLHTTP can be created when XMLHTTP is obtained, and the used XMLHTTP cannot be retrieved from the pool, because the readystate of the used XMLHTTP is 4, therefore, both synchronous and asynchronous requests send but onreadystatechange is not executed.

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.