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.