Ajax Study Notes (1)

Source: Internet
Author: User

The core of the technology is to use XMLHTTP to request and receive responses. XMLHTTP is only a technical specification. The specific implementation of IE is different from that of Firefox. IE is implemented in ActiveX mode, while Firefox is implemented in Firefox.

Create an XMLHTTP object in IE:
A = new activexobject ('msxml2. xmlhttp ');

Create an XMLHTTP object in Firefox:
A = new XMLHttpRequest ();

You can also use the following method to better be compatible with IE and Firefox:
Function createxh (){
VaR A = NULL;
Try {A = new activexobject ('msxml2. xmlhttp')} catch (e ){
Try {A = new activexobject ('Microsoft. xmlhttp')} catch (OC) {A = NULL}
}
If (! A & typeof XMLHttpRequest! = 'Undefined') {A = new XMLHttpRequest ()}
Return
}

After creating XMLHTTP, the next step is to send a request and receive a response:
VaR XMLHTTP = createxh ();
XMLHTTP. Open ('post', 'message/checknew. aspx ',False);
False-the message is always waiting for response (synchronous execution) after being sent );
True-the message is sent without waiting for a response. The next step (that is, asynchronous execution) is continued );

The following is the synchronous executionCodeStatement:
XMLHTTP. Open ('post', 'message/checknew. aspx ', false );
XMLHTTP. Send (''); // You can also send some data in the request. If no data exists, you must also send an empty data.
VaR RT = XMLHTTP. responsetext;
// Rt is the response data, followed by the client's analysis and processing of RT

The synchronous execution method is generally not recommended, because if the request server does not respond for half a day or the network is unstable, the browser will wait for a response, and the browser will almost die while waiting, the CPU usage is close to 100%!
This problem can be avoided through asynchronous execution as follows:
VaR XMLHTTP = createxh ();
Function checknewmessages (){
XMLHTTP. Open ('post', 'message/checknew. aspx ', true );
XMLHTTP. onreadystatechange = continue;
XMLHTTP. Send ('');
}
Function continue (){
If (XMLHTTP. readystate = 4 ){
VaR RT = XMLHTTP. responsetext;
}
}

How to handle the returned strings of XMLHTTP. responsetext? Some Ajax frameworks encapsulate the processing process, add many data types, and even process dataset. If you do not want to use these frameworks, you can also use simple types. For example, you can format the request data in XML format (to facilitate server-side aspx processing ), the returned result is written as an array expression of JavaScript (which is convenient for the client to process ).

example:
when sending
var xml = ' Kathy message context! ';
...
XMLHTTP. send (XML);
the string returned by the server upon receiving is:
['rkeyy', '2017-05-01 08:22:12 ', 'See you! ']
var RT = XMLHTTP. responsetext;
var MSG = eval (RT);
data can be processed in array mode.

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.