In web script programming, the asynchronous mode should be used in most cases; the synchronous mode will suspend the current script engine, so when you use the synchronous mode, you should understand what you want. In C ++ development, the synchronization mode should be the mainstream. If you must use the asynchronous mode and callback, you can refer to using ixmlhttprequest onreadystatechange from C ++.
The following shows how to obtain RSS files on a remote host in asynchronous mode:CodeThe key is to set a callback function to ixmlhttprequest: onreadystatechange. To prevent the script from returning to the console too early, the asyncdone variable is used to check the current status. Of course, if XMLHTTP is used in a webpage, you don't have to worry about it-as long as the IE webpage is not closed, the callback function will not exit.Copy codeThe Code is as follows: var XMLHTTP = new activexobject ("msxml2.xmlhttp. 6.0 ");
VaR url = "http://www.jb51.net/rss.xml ";
VaR asyncdone = false;
Try {
XMLHTTP. Open ("get", URL, true );
XMLHTTP. onreadystatechange = onreadystatechange;
XMLHTTP. Send (null );
// Loop so that the program from quiting
While (! Asyncdone ){
Wscript. Sleep (100 );
}
Wscript. Echo (XMLHTTP. responsetext );
} Catch (e ){
Wscript. Echo (E );
}
Function onreadystatechange (){
Wscript. Echo ("readystate:" + XMLHTTP. readystate );
If (XMLHTTP. readystate = 4 ){
Asyncdone = true;
}
}
The code for obtaining remote host resources in synchronization mode is much simpler:
Copy code The Code is as follows: var XMLHTTP = new activexobject ("msxml2.xmlhttp. 6.0 ");
VaR url = "http://www.jb51.net/rss.xml ";
Try {
XMLHTTP. Open ("get", URL, false );
XMLHTTP. Send (null );
Wscript. Echo (XMLHTTP. responsetext );
} Catch (e ){
Wscript. Echo (E );
}
However, if the synchronization mode is used in IE, the script will be suspended until XMLHTTP returns because the callback mechanism is absent and IE does not support script thread opening. Note that the IE interface is suspended.
Synchronous or asynchronous. The specific problem must be analyzed.