How Ajax works and how asynchronous requests are encapsulated _ajax related

Source: Internet
Author: User
Ajax principles:
The client sends an asynchronous request through the browser's built-in object XMLHttpRequest, and when the server returns a response, the previously registered callback function is invoked, and the JavaScript action dom can be used to update the page in the callback function, and the asynchronous request does not block the client's operation. The effect of updating data can be achieved if the page cannot be refreshed.
Encapsulation of an asynchronous request:
Copy Code code as follows:

var Xhr=false;
Step1: Create a XMLHttpRequest object that is compatible with each version of the browser
if (window. XMLHttpRequest) {//ie7+, Firefox, Chrome, Opera, Safari
XHR = new XMLHttpRequest ();
} else {
if (window. ActiveXObject) {//ie browser
XHR = new ActiveXObject ("Microsoft.XMLHTTP");//ie5+
}
}
Step2: Set callback function
Xhr.onreadystatechange = Mycallback;
Step3: Create an asynchronous request
Xhr.open ("Method", "url", true);
If it is post: To set the encoding of the request message body data
Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
STEP4: Sending an asynchronous request
Xhr.send (content); If the content of the Get method is null, the post,content is a "name = value" Pair.
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.