但异步请求提交后,服务端完成请求,dwr框架会调用DWREngine._stateChange来处理返回结果
/**
* @private Called by XMLHttpRequest to indicate that something has happened
*/
DWREngine._stateChange = function(batch) {
//状态正确则进入下面的if块
if (!batch.completed && batch.req.readyState == 4) {
try {
//取回返回的文本
var reply = batch.req.responseText;
//请求的状态
var status = batch.req.status;
//返回为空或空串则抛意外
if (reply == null || reply == "") {
DWREngine._handleMetaDataError(null, "No data received from server");
return;
}
// This should get us out of 404s etc.
//返回串中如果不包含"DWREngine._handle"也抛意外
if (reply.search("DWREngine._handle") == -1) {
DWREngine._handleMetaDataError(null, "Invalid reply from server");
return;
}
//状态不对,抛意外
if (status != 200) {
if (reply == null) reply = "Unknown error occured";
DWREngine._handleMetaDataError(null, reply);
return;
}
//执行返回的文本,返回的文本实际上是一段脚本
eval(reply);
// We're done. Clear up
//完成之后坐清理
DWREngine._clearUp(batch);
}
catch (ex) {
if (ex == null) ex = "Unknown error occured";
DWREngine._handleMetaDataError(null, ex);
}
finally {
// If there is anything on the queue waiting to go out, then send it.
// We don't need to check for ordered mode, here because when ordered mode
// gets turned off, we still process *waiting* batches in an ordered way.
//如果_batchQueue还有任何等待的请求,继续执行
if (DWREngine._batchQueue.length != 0) {
var sendbatch = DWREngine._batchQueue.shift();
DWREngine._sendData(sendbatch);
DWREngine._batches[DWREngine._batches.length] = sendbatch;
}
}
}
};
总结:
重点看看到底DWR返回什么,
下面是个真实的例子:
"var s0={};
var s1=/"666<br><br>ttt/";
s0.content=s1;
var s2=null;
s0.crttime=s2;
var s3=632;
s0.id=s3;
var s4=/"127.0.0.1/";
s0.ip=s4;
var s5=0;
s0.parentId=s5;
var s6=false;
s0.replied=s6;
var s7=/"/";
s0.title=s7;
var s8={};
var s9=false;
s8.anonymous=s9;/n
var s10=null;
s8.createtime=s10;
var s11=/"alex/";
s8.loginName=s11;
。。。。。。。
DWREngine._handleResponse('8781_1152243284156', s0);/n"
原来返回的字符串是一段脚本,脚本分为两部分,前一部分是生成一个js对象,对应服务端返回的对象模型,
后一段是调用DWREngine._handleResponse,通过eval函数来执行这段脚本。