First look at the call method:
CopyCode The Code is as follows: Ajax. Request ("ajax.html", {v: Math. Random (), num: 1}, function (data ){
// Do something
}, 'Get ');
The method is like jquery... I still think this call is more convenient...Copy codeThe Code is as follows: var Ajax = {
// XMLHttpRequest class
XMLHttpRequest: function (){
This. xhr = false;
},
// External call interface
Request: function (URL, Data, callback, type ){
// Create an XMLHTTPRequest object each time, so that AJAX calls do not affect each other
VaR xhr = new this. XMLHttpRequest ();
Xhr. Request (URL, Data, callback, type );
}
}
// Convert the JSON data format {num: 1, t: 'A'} to the string format num = 1 & t =.
VaR json2str = function (data ){
VaR _ DATA = [];
For (VAR name in data ){
_ Data. Push (name + "=" + data [name]);
}
Return _ data. Join ('&');
}
// Method for extending the XMLHttpRequest class
Ajax. XMLHttpRequest. Prototype = {
// Create XMLHttpRequest
Createxmlhttprequest: function (){
If (window. XMLHttpRequest ){
Return new XMLHttpRequest ();
}
Else {
VaR A = ["msxml2.xmlhttp", "Microsoft. XMLHTTP", "msxml2.xmlhttp. 5.0", "msxml2.xmlhttp. 4.0", "msxml2.xmlhttp. 3.0"];
For (VAR I = 0, L = A. length; I <L; I ++ ){
Try {
Return new activexobject (A [I]);
} Catch (e ){};
}
}
},
// Callback function
Fncallback: function (callback ){
If (this. xhr. readystate ===4 & this. xhr. Status === 200 ){
Callback? Callback (this. xhr. responsetext): void (0 );
}
},
// Ajax request
Request: function (URL, Data, callback, type ){
VaR that = this;
VaR ispost = type = 'post '? True: false;
Ispost? URL: URL + = '? '+ Json2str (data );
This. xhr = This. createxmlhttprequest ();
This. xhr. Open (type, URL, true );
Ispost? This. xhr. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded "):'';
This. xhr. onreadystatechange = function () {That. fncallback (callback );};
This. xhr. Send (ispost? Json2str (data): NULL );
}
}
There must be some shortcomings in this category. Please click it! Everyone has their own habits. The most important thing is that you can use them!