|
阅读:4489回复:3
写AJAX的onreadystatechange Handle遇到的问题function processReqChange() {
// only if req shows "loaded"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
// ...processing statements go here...
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}
这里 req 对象必须是一个全局对象,我自己写了一个lib, 直接用 var wajax = new WebAJAX(); 方法获取一个实例,其中 wajax.core 为 req 对象,因为可能同时产生多个WebAJAX对象,所以不能用同一个processReqChange作为handle,请问如何既不用不重复写handle方法也能同时调用多个handle呢? |
|
|
|
1楼#
发布于:2005-07-12 16:15
null
|
|
|
|
2楼#
发布于:2005-07-12 16:15
已自行解决,用eval
/* 设置提交方法 */
WebAJAX.prototype.setReqFunc = function(oFunc) {
var _oFunc = "funcWebAJAX = " + "function(){}";
if(typeof oFunc == "undefined" || null == oFunc)
_oFunc = "funcWebAJAX = " + "function(){}";
else if(typeof oFunc == "function")
_oFunc = "funcWebAJAX = " + oFunc.toString();
else if(typeof oFunc == "string")
_oFunc = oFunc;
this.reqFunc = _oFunc;
}
/* 执行请求 */
WebAJAX.prototype.doQuery = function() {
try {
var wajaxCore = this.core;
this.core.onreadystatechange = eval(this.reqFunc);
this.core.open(this.reqType, this.reqURL, true);
this.core.send();
} catch (e) {
WebUtil.debug("do query failed ...");
}
}
function handle1() {
...........................
}
function handle2() {
...........................
}
var arrWajax = new Array();
arrWajax[1] = new WebAJAX(url1, handle1);
arrWajax[2] = new WebAJAX(url2, handle2);
.......
arrWajax[1].doQuery();
......
arrWajax[2].doQuery();
...... |
|
|
|
3楼#
发布于:2005-07-12 16:15
目前唯一的方法是
/* 执行请求 */
WebAJAX.prototype.doQuery1 = function() {
try {
var ajaxReq = this.core;
// this.core.onreadystatechange = this.reqFunc;
this.core.onreadystatechange = function () {
var r = false;
// only if req shows "loaded"
if (ajaxReq.readyState == 4) {
// only if "OK"
if (ajaxReq.status == 200) {
r = true;
// ...processing statements go here...
} else {
// alert("There was a problem retrieving the XML data:\n" +
// req.statusText);
}
}
alert("readAJAXResult = " + r);
}
this.core.open(this.reqType, this.reqURL, true);
this.core.send();
} catch (e) {
WebUtil.debug("do query failed ...");
}
}
WebAJAX.prototype.doQuery2 = function() { ...
像这样先定义多个请求方法,然后new出来的对象调不同的方法 |
|
|