Ajax journey (2) -- XMLHttpRequest

Source: Internet
Author: User

The asynchronous update of Ajax mentioned above is mainly implemented using the XMLHTTPRequest object. the XMLHTTPRequest object can implement partial update of webpages without submitting the entire page to the server. After all the pages are loaded, the client requests data from the server through this object. After the server receives and processes the data, it reports the data to the client. The methods and attributes of the XMLHTTPRequest object are as follows (this is not the focus of this article ):

<HTML> 

To have a better understanding of the XMLHTTPRequest object, we only know that its method and attributes are only the basis of the object. We also need to understand its creation and use process. The following describes how to create and use the XMLHTTPRequest object summarized and analyzed in a demo. For more information, see!

 

1.First,Use the createxmlhttprequest method to create an XMLHTTPRequest object. The simple branch logic is used to determine how to create objects. Determine whether the browser supports ActiveX controls and whether the browser is IE.

2.Second,Use the open method to create a call to the server and set the URL and the expected HTTP method, usually get or post. For the difference between get and post, see:


3.Furthermore,After a general-purpose server, we need to complete a task to call the handlestatechange function when the server returns the required data or the XMLHttpRequest status changes. XMLHTTP. onreadystatechange = handlestatechange;

4. Then,After "where to go (call to the server)" and "where to return (handlestatechange function)" are set, the request can be sent. XMLHTTP. Send (xmldoc );

5.Finally,You need the handlestatechange function to check the readystate attribute of XMLHttpRequest, and then check the status code returned by the server. If everything is normal, the handlestatechange function will perform corresponding operations on the client. This is the so-called callback function.

 

As mentioned above, how does XMLHttpRequest Implement Asynchronous update for the use of Ajax core object? This is reflected in the callback function we mentioned above. Let's take a look at the calling sequence of the above program. in step 3, we set the function to be called when the server returns data. After the setting is complete, we don't have to worry about it, you can continue with the next step (for example, step 4). During the operation, once the status of XMLHttpRequest changes, the handlestatechange function is automatically called to implement the function. In this process, asynchronous updates are easily implemented.

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.