The following is the first draft of the Easy. Ajax class. If it must be published, it must be modified in the code to minimize it, but the API will not change.
The Code is 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 ',
Revoke Ache: 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.contentwindodoc ument | iframe. contentDocument | your frames[id=.doc ument;
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 )();
}
};