Ajax synchronous and asynchronous XMLHTTP code analysis

Source: Internet
Author: User

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.

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.