Easy.Ajax 部分源代码, 支持文件上传功能, 兼容所有主流浏览器
下面是Easy.Ajax类的初稿,如须发表,在代码上还要修改以达到最简,但API是不会变了,
Easy.Ajax = (function (WINDOW) {
ajax = {
proxyPool: {
length: function () {
var i = 0;
for (var p in this)
i++;
return i - 1;
}
},
index: 0,
clearCache: true,
contentType: "application/x-www-form-urlencoded; charset=utf-8",
timeout: 10000,
async: true,
xmlData: false,
createXhr: function (id) {
var py, pxy;
try {
try {
pxy = new XMLHttpRequest();
} catch (e) {
}
var md = ['Msxml2.XMLHTTP.4.0', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP'];
for (var i = 0; !pxy && i < md.length; 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');
}
},
getParam: function (pms) {
return Easy.util.join(pms, "&");
},
callback: function (rsp, cfg) {
if (this.status == 200) {
(cfg.success || Easy.emptyFn).call(this, rsp);
} else {
(cfg.failure || Easy.emptyFn).call(this, rsp, this.statue);
}
},
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, uname, pwd);
conn.setRequestHeader("Content-Type", cfg.xmlData || this.xmlData
? "text/xml"
: this.contentType);
conn.setRequestHeader("timeout", cfg.timeout || this.timeout);
try {
conn.setRequestHeader("X-Requested-With", "XMLHttpRequest");
} catch (e) {
}
try {
conn.onreadystatechange = function () {
if (conn.readyState == 4) {
pxy.isLoading = false;
(cfg.callback || me.callback).call(conn, conn.responseText
|| conn.responseXML, cfg);
Easy.delay(me.destroy, 10, pxy, pxy.id);
}
}
conn.ontimeout = cfg.ontimeout || dh.ontimeout;
} catch (e) { }
return pxy;
},
toRequstCfg: function (cfg) {
if (Easy.isString(cfg))
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) {
if (cfg.isUpload && cfg.form)
return this.upload(cfg);
var pxy = this.open(method || "POST", cfg.url, true, cfg), proxy = pxy.conn;
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);
return this.request(cfg, "GET");
},
post: function (cfg) {
cfg = this.toRequstCfg(cfg);
return this.request(cfg);
},
upload: function (cfg) {
if (!form) {
form = Easy.DOM.create({
tag: 'form',
id: "Easy_Ajax_Form"
}, Easy.getBody());
formCreated = true;
}
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.isIE6 || Easy.isIE7)
document.frames[iframeID].name = iframeID;
var complete = function () {
Easy.DOM.destroy(iframe);
if (formCreated)
Easy.DOM.destroy(form);
};
cfg.url = cfg.url || form.action;
Easy.DOM.setAttributes(form, {
action: Easy.util.urlAppend(cfg.url, cfg.params),
target: iframeID,
enctype: "multipart/form-data",
encoding : "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, 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) {
var ajax = Easy.Ajax
ajax.abort.call(ajax, id);
delete ajax.proxyPool[id];
},
abort: function (id) {
if (!Easy.isIE6)
try { (this.proxyPool[id] || { conn: {} }).conn.abort(); } catch (e) { }
}
}
return ajax;
})(window);