Copy Code code as follows:
Easy.ajax = {
Proxypool: {
Length:function () {
var i = 0;
For (var p in this)
i++;
return i-1;
}
},
index:0,
Async:true,
Xmldata:false,
Timeout:1,
Defaultheader: ' Application/json; Charset=utf-8 ',
Clearcache:true,
Emptyfn:function () {
},
Defaulthandlers: {
Empty:function () {},
Onerror:this.empty,
Onload:this.empty,
Ontimeout:this.empty,
Onprogress:this.empty
},
Createxhr:function (ID) {
var py, Pxy;
try {
var MD = ["Msxml2.xmlhttp", "Microsoft.XMLHTTP"];
try {
Pxy = new XMLHttpRequest ();
catch (e) {
}
if (!pxy && window. Xdomainrequest)
Pxy = new Xdomainrequest ();
for (var i = 0;!pxy i++)
try {
Pxy = new ActiveXObject (Md[i]);
catch (e) {
}
PY = {
Conn:pxy,
Isloading:false,
Id:id
};
This.proxypool[id] = py;
catch (e) {
return new Easy.error (e, e.message);
finally {
Return PXY? Py:new easy.error (' Null pointer ');
}
},
Setevents:function (Pxy, CFG, override) {
try {
var dh = this.defaulthandlers, props = CFG, conn = Pxy.conn, me = this;
For (var p in DH) {
if (!override && Conn.hasownproperty (p))
Continue
try {
CONN[P] = Props[p] | | DH[P];
catch (e) {
}
}
Conn.onreadystatechange = function () {
if (conn.readystate = = 4) {
Pxy.isloading = false;
(Cfg.callback | | me.callback). CALL (Conn, conn.responsetext
|| Conn.responseXML.xml, CFG);
Me.destroy (pxy.id);
}
}
catch (e) {
finally {
Return conn;
}
},
Callback:function (RSP, CFG) {
var emptyfn = function () {
};
if (This.status = = 200) {
(cfg.success | | emptyfn). Call (this, RSP);
} else {
(Cfg.failure | | emptyfn). Call (this, RSP, this.statue);
}
},
Getparam:function (PMS) {
Return Easy.util.join (PMS, "&");
},
Open:function (method, URL, async, cfg, uname, pwd) {
var me = this, Pxy = THIS.CREATEXHR (this.index++);
var conn = Pxy.conn;
Conn.Open (method, URL, async);
Conn.setrequestheader ("Content-type", Cfg.xmldata | | this.xmldata
? "Text/xml"
: This.defaultheader);
Conn.setrequestheader ("timeout", this.timeout);
return pxy;
},
Torequstcfg:function (CFG) {
if (Easy.gettype (cfg) = = "string")
CFG = {
Url:cfg
};
Cfg.url = Easy.util.urlAppend (Cfg.url, Math.random (5))
var form = Easy.DOM.get (Cfg.form);
if (form) {
if (Cfg.isupload | |/multipart\/form-data/i.test (form.getattribute ("enctype"))
Cfg.isupload = true;
Else
Cfg.params = Easy.util.serializeForm (form);
}
return CFG;
},
Request:function (cfg, method) {
var Pxy = This.open (Method | | "POST", Cfg.url, True, cfg), proxy = Pxy.conn;
Proxy = this.setevents (Pxy, CFG, true);
var params = This.getparam (cfg.params), bl = cfg.beforeload;
if (BL && easy.gettype (BL) = = "function" && bl.call (proxy) = False)
Return
Proxy.send (params);
Pxy.isloading = true;
return pxy.id;
},
Get:function (CFG) {
CFG = this.torequstcfg (CFG);
if (cfg.isupload)
return This.upload (CFG);
Return This.request (CFG, "get");
},
Post:function (CFG) {
CFG = this.torequstcfg (CFG);
if (cfg.isupload)
return This.upload (CFG);
return this.request (CFG);
},
Upload:function (CFG) {
var form = Easy.DOM.get (Cfg.form);
var iframe = document.createelement ("iframe");
var Iframeid = "Easy_ajax_form_submit";
Easy.DOM.setAttributes (IFRAME, {
Id:iframeid,
Name:iframeid,
Width: "0px",
Height: "0px",
Style: "Display:none;",
SRC: "About:blank"
});
Easy.DOM.render (iframe, form);
if (Easy.util.isIE)
Document.frames[iframeid].name = Iframeid;
var complete = function () {
Easy.DOM.destroy (IFRAME);
};
Cfg.url = Cfg.url | | Form.action;
Easy.DOM.setAttributes (Form, {
Action:Easy.util.urlAppend (Cfg.url, Cfg.params),
Target:iframeid,
Enctype: "Multipart/form-data",
Method: "POST"
});
var cb = function () {
try {
var me = this, R =
{
ResponseText: ", Responsexml:null
},
Doc
FirstChild;
try {
Doc = Iframe.contentWindow.document | | iframe.contentdocument | | Window.frames[id].document;
if (DOC) {
if (doc.body) {
if (/textarea/i.test (firstchild = Doc.body.firstChild | | {}). TagName)) {
R.responsetext = Firstchild.value;
}
else {
R.responsetext = Doc.body.innerHTML;
}
}
R.responsexml = R.responsetext;
}
}
catch (e) {
}
(Cfg.callback | | cfg.success | | complete). Call (R, R.responsetext | |
R.responsexml.xml, CFG);
catch (e) {
(cfg.failure | | cfg.callback | | complete). Call (R, E.message, CFG);
}
};
Easy.DOM.on (iframe, "load", CB, IFRAME);
Form.submit ();
},
Destroy:function (ID) {
This.abort (ID);
Delete This.proxypool[id];
},
Abort:function (ID) {
if (! EASY.UTIL.ISIE6)
((This.proxypool[id] | | {}). Conn.abort) | | THIS.EMPTYFN) ();
}
};