//MyAjaxObj class encapsulating XMLHTTP
var MyAjaxObj = new Object();
var maxXmlHttpCount = 5; //The maximum number of 5 xmlhttp objects exist
= []; //You can clear the items inside
= function() {
var req = null;
var len = ;
//First take it from the current pool
for (var i = 0; i < len; i++) {
if ([i]) {
if ([i].readyState == 4 || [i].readyState == 0) {
req = [i];
break;
}
}
}
//If there are no idle objects, create them independently
if (req == null) {
if ( < maxXmlHttpCount) {
req = getXmlHttp();
(req);
}
}
return req;
}
//Create an XMLHTTP object, compatible with different browsers
function getXmlHttp() {
var xmlHttp = false;
var arrSignatures = [".5.0", ".4.0",
".3.0", "",
""];
for (var i = 0; i < ; i++) {
try {
xmlHttp = new ActiveXObject(arrSignatures[i]);
return xmlHttp;
}
catch (oError) {
xmlHttp = false; //ignore
}
}
// throw new Error("MSXML is not installed on your system.");
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
/*The operation of encapsulating XMLHTTP sending requests to the server
url: the path requested to the server; method: the requested method, that is, get/post; ***callback: the function called when the server successfully returns the result (similar to the c# callback function)***
data: the data attached when requesting to the server; urlencoded: whether the url is encoded; cached: whether to use cache; callBackError; function called when the server returns an error
*/
= function(url, method, callback, data, urlencoded, cached, callBackError) {
var req = (); //Instantiate an XMLHTTP instance from the pool or directly
//Called when the request status of XMLHTTP changes (core processing function)
= function() {
// When the request has been loaded
if ( == 4) {
// When the request returns successfully
if ( == 200) { //or < 400
// When a successful callback function is defined, the successful callback function is executed
if (callback)
callback(req, data);
}
// When the request returns an error
else {
// When a failed callback function is defined, the failed callback function is executed
if (callBackError)
callBackError(req, data);
}
// With pool management, we can save the method of releasing resources
// try {
// delete req;
// req = null;
// }
// catch (e) {
// alert();
// }
}
}
//If you post back to the server in POST mode
if (() == "POST") {
("POST", url, true);
//Whether the request needs to be cached (this item can only be set later)
if (cached)
("If-Modified-Since", "0");
//Requests need to be encoded
if (urlencoded)
('Content-Type', 'application/x-www-form-urlencoded');
(data);
(req);
}
//Request by GET
else {
("GET", url, true);
//Whether the request needs to be cached
if (cached)
("If-Modified-Since", "0");
(null);
(req);
}
return req;
}
//Clear all XMLHTTP array elements and release resources
= function() {
var len = ;
for (var i = 0; i < len; i++) {
var req = [i];
if (req) {
try {
delete req;
} catch (e) { }
}
}
= [];
}
//Further encapsulate the code when XMLHTTP sends requests in POST mode
//isClear: Whether to clear all elements of the XMLHTTP array; see the meaning of other parameters.
= function(url, data, callback, isClear, isCached, callBackError) {
if (isClear) {
();
}
(url, "POST", callback, data, true, isCached, callBackError); //The post method requires encoding
}
//Further encapsulate the code when XMLHTTP sends requests in GET
= function(url, args, callback, isClear, isCached, callBackError) {
if (isClear)
();
return (url, "GET", callback, args, false, isCached, callBackError);
}