How to compile an Ajax application using XMLHttpRequest

Source: Internet
Author: User

It can be roughly divided into the following five steps:

1) Create an XMLHTTPRequest object. Note that the creation methods for different browsers are different:

For Firefox, javasillar, opera, Safari, IE7, and IE8, the creation method is as follows:

XMLHttpRequest XMLHTTP = new XMLHttpRequest ();

For IE6, ie5.5, and ie5, the creation method is as follows:

VaR activexname = ["msxml2.xmlhttp", "Microsoft. XMLHTTP"];
For (VAR I = 0; I <activexname. length; I ++ ){
Try {
// Retrieve a control name for creation. If the creation is successful, terminate the loop.
// If creation fails, an exception is thrown back. You can continue the loop and try again.
XMLHTTP = new activexobject (activexname [I]);
Break;
} Catch (e ){
}

 

 

2) register the callback function

XMLHTTP. onreadystatechange = callback;

 

The callback function name is assigned to onreadystatechange. Do not write it as Callback (). The latter indicates that the callback function execution result is assigned a value.

 

3) create connection information, that is, set the submission method, server URL, and whether asynchronous communication is adopted. For the get submission method, you can splice parameters in the URL.

XMLHTTP. Open ("get", "serverurl? Name = "+ username, true );

 

4) submit data to the server and start Interaction

XMLHTTP. Send (null );

 

If the get method is used in step 3, the submitted parameters have been spliced in the URL, so a null value can be passed during data transmission.

 

 

5) receive data from the server. Determine whether the interaction is complete before obtaining data.

If (XMLHTTP. readystate = 4 ){
// Determine whether HTTP interaction is successful
If (XMLHTTP. Status = 200 ){
... Start to get the data returned by the paint service.

 

The value of readystate is as follows:

0: the request has not been initialized (open () has not been called ()).
1: The request has been created but has not been sent (send () has not been called ()).
2: The request has been sent and is being processed (the content header can be obtained from the response ).
3: The request is being processed. Generally, some data in the response is available, but the server has not yet completed the response generation.
4: The response has been completed. You can obtain and use the server response.

 

If jquery is usedCodeWill be much reduced. For example, the browser judgment in step 1 is completed by jquery.

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.