jquery's Ajax:ajax tool class
Last Update:2017-02-28
Source: Internet
Author: User
var ajax={
Xhr:null,
Ok:true,
Xhrpool: [],
Createxhrobj:function () {
var xhrobj;
try {
if (window. XMLHttpRequest) {
Xhrobj = new XMLHttpRequest ();
}else if (window. ActiveXObject) {
var MSXML = [' MSXML2. xmlhttp.5.0 ', ' MSXML2. xmlhttp.4.0 ', ' MSXML2. xmlhttp.3.0 ', ' MSXML2. XMLHTTP ', ' microsoft.xmlhttp ', ' MSXML2. xmlhttp.6.0 '];
for (Var i=0;i<msxml.length;i++) {
try{
Xhrobj = new ActiveXObject (Msxml[i]);
Break
}catch (e) {
}
}
}
if (xhrobj.readystate = null)
{
xhrobj.readystate = 0;
Xhrobj.addeventlistener ("Load", function () {
Xhrobj.readystate = 4;
if (typeof Xhrobj.onreadystatechange = = "function")
{
Xhrobj.onreadystatechange ();
}
}, False);
}
return xhrobj;
catch (e) {
Alert ("XMLHttpRequest Create Error!");
}
},
Getxhrinstance:function () {
for (Var i=0;i<this.xhrpool.length;i++) {
if (this.xhrpool[i].readystate = 0 This.xhrpool[i].readystate = = 4)
{
return this.xhrpool[i];
}
}
This.xhrpool[this.xhrpool.length] = This.createxhrobj ();
return this.xhrpool[this.xhrpool.length-1];
},
Init:function () {
This.xhrpool = new Array ();
},
Setheader:function (key, value) {
var xhrobj = this.xhr;
try{
Xhrobj.setrequestheader (Key,value);
}catch (e) {
Alert ("Set header exception:" +e.message);
}
},
Parsing data by type
_parse:function (Text,dtype) {
var _data = null;
if (Dtype = = ' JSON ') {
_data = text;
Return eval (' (' + _data + ') ');
}
return text;
},
Cbobj must contain callback functions
Senddataforjson:function (data, Url,method, cbobj) {
try{
var xhrobj = this.getxhrinstance ();
var msg= "";
for (i=0;i<data.length;i++) {
var obj = data[i];
if (i = = data.length-1) {
var v = obj.value;
msg + + obj.key+ "=" +encodeuricomponent (encodeURIComponent (v));
}else{
var v = obj.value;
msg + + obj.key+ "=" +encodeuricomponent (encodeURIComponent (v)) + "&";
}
}
if (method = = "Get") {
Xhrobj.open (method,url+ "randnum=" + math.random () + "&" +msg,true);
Xhrobj.setrequestheader ("Content-type", "text/html");
Xhrobj.setrequestheader ("Cache-control", "No-cache");
Xhrobj.setrequestheader ("Customdatatype", "JSON");
Xhrobj.send (NULL);
}else if (method = = "POST") {
Xhrobj.open (method,url+ "? randnum=" +math.random (), true);
Xhrobj.setrequestheader ("Content-type", "application/x-www-form-urlencoded");
Xhrobj.setrequestheader ("Cache-control", "No-cache");
Xhrobj.setrequestheader ("Content-length", msg.length);
Xhrobj.setrequestheader ("Connection", "close");
Xhrobj.setrequestheader ("Customdatatype", "JSON");
Xhrobj.send (msg);
}
var returntext = "";
Xhrobj.onreadystatechange = function () {
if (xhrobj.readystate = = 4) {
if (Xhrobj.status = = 200) {
Returntext = Xhrobj.responsetext;
Returntext = decodeURIComponent (decodeURIComponent (Returntext));
Cbobj.callback (Ajax._parse) (Returntext,xhrobj.getresponseheader ("Customdatatype"));
}
}
Xhrobj.abort ();
};
return returntext;
}catch (e) {
Alert (' Connection server exception, error reason: ' + e.name + ' Description: ' +e.message ');
Xhrobj.abort ();
}
}
}
This article links http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20121127/34462.html