In many cases, Ajax xmlHttpRequest requests are not concurrent, so you can call them directly in JS or call methods in jquery;
However, if the request concurrency is considered, and the time is long and the frequency is high, the connection pool problem needs to be considered;
The following is the Ajax connection pool code:
Code:Var XMLHttp = {
_ ObjPool: [],
_ GetInstance: function (){
For (var I = 0; I <this. _ objPool. length; I ++ ){
If (this. _ objPool [I]. readyState = 0 | this. _ objPool [I]. readyState = 4 ){
Return this. _ objPool [I];
}
}
// The push method is not supported in IE5.
This. _ objPool [this. _ objPool. length] = this. _ createObj ();
Return this. _ objPool [this. _ objPool. length-1];
},
_ CreateObj: function (){
If (window. XMLHttpRequest ){
Var objXMLHttp = new XMLHttpRequest ();
}
Else {
Var MSXML = ['msxml2. XMLHTTP.5.0 ', 'msxml2. XMLHTTP.4.0', 'msxml2. XMLHTTP.3.0 ', 'msxml2. xmlhttp', 'Microsoft. xmlhttp'];
For (var n = 0; n <MSXML. length; n ++ ){
Try {
Var objXMLHttp = new ActiveXObject (MSXML [n]);
Break;
}
Catch (e ){
}
}
}
// Some mozilla versions do not have the readyState attribute
If (objXMLHttp. readyState = null ){
ObjXMLHttp. readyState = 0;
ObjXMLHttp. addEventListener ("load", function (){
ObjXMLHttp. readyState = 4;
If (typeof objXMLHttp. onreadystatechange = "function "){
ObjXMLHttp. onreadystatechange ();
}
}, False );
}
Return objXMLHttp;
},
// Send the request (method [post, get], address, Data, callback function)
SendReq: function (method, url, data, callback ){
Var objXMLHttp = this. _ getInstance ();
With (objXMLHttp ){
Try {
// Add random numbers to prevent caching
If (url. indexOf ("? ")> 0 ){
Url + = "& randnum =" + Math. random ();
}
Else {
Url + = "? Randnum = "+ Math. random ();
}
Open (method, url, true );
Setrequestheader ("cache-control", "no-cache"); // The request page is not cached.
SetRequestHeader ('content-type', 'application/x-www-form-urlencoded; charset = UTF-8 '); // sets the Request Encoding Method
Send (data );
Onreadystatechange = function (){
If (objXMLHttp. readyState ==4 & (objXMLHttp. status == 200 | objXMLHttp. status == 304 )){
Callback (objXMLHttp );
Url = null;
Data = null;
Callback = null;
Method = null;
CollectGarbage ();
}
}
}
Catch (e ){
Alert ("Script Error:" + e );
}
}
}
};
Call example:
// Send information
Function sendMessage (username, uid, data1, user_to ){
If (isNaN (uid) return;
Var data = "[data] =" + escape (data1 );
Var url = "Ajax_method.aspx? Method = sendMessage & id = "+ uid +" & username = "+ escape (username) +" & user_to = "+ user_to;
XMLHttp. sendReq ("post", url, data, function (request ){
Var state = request. responseText;
If (state = "shibai "){
Alert ("sending information failed ...");
}
});
}