In this paper, we analyze the native Ajax notation. Share to everyone for your reference. The specific analysis is as follows:
Although many JS frameworks now provide Ajax call methods, we do not even have to understand their specific implementation of the principle can be directly invoked, but I do not think from the most basic level to understand a thing, it is difficult to do the best.
var xhr=null;
if (window. XMLHttpRequest) {
//non ie kernel
XHR = new XMLHttpRequest ();
} else if (window. ActiveXObject) {
//IE kernel, where earlier versions of IE are written differently, can be queried under
XHR = new ActiveXObject ("Microsoft.XMLHTTP");
} else {
XHR = null;
}
if (XHR) {
xhr.open ("Get", "ajaxserver.action");
Xhr.onreadystatechange = function () {
///readystate Value Description
//0, initialization, XHR object already created, open
//1 not executed, loading, already called Open method , but did not send the request
//2, loading completed, request has been sent to complete
//3, interactive, can receive some
data/Status Value Description
//200: Success
//404: No files, queries or URLs found
//500: Server generates internal error
if (xhr.readystate = = 4 && Xhr.status = =) {//
here can handle the returned content
///generally return JS On or XML data format
Console.log (xhr.responsetext);
Active release, JS itself will also be recycled
XHR = null;
}
;
Xhr.send ();
}
I hope this article will help you with your AJAX programming.