jquery's Ajax:ajax tool class

Source: Internet
Author: User
Tags json key return window

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

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.