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.