日期:2014-05-16  浏览次数:20458 次

ExtJs 中的Ext.Ajax.request总结

?????? 费了九牛二虎之力测试了一下这个东东,有学习的供参考!多交流!嘿嘿。。。

第一种方式:

//后台返回值:
{"data":{"bookStatusId":20,"returnTime":"2011-03-05","bookSN":"123456-1","lendTime":"2011-03-02 11:20",
"borrower":"李四","bookId":61,"borrowerId":6,"lendOutId":34,"bookName":"1"},"success":true }

//前台解析:

Ext.Ajax.request({
??????????????????????????????? url : _ctx.base + '/administration/bRet_loadBooksLendOutInfo.action',
??????????????????????????????? params : { ID : comboBox.getValue()},
??????????????????????????????? success : function(response, options){
??????????????????????????????????? var resp = Ext.decode(response.responseText);
??????????????????????????????????? alert("resp:"+resp+"/value:"+resp.data.borrower);
??????????????????????????????? },

??????????????????????????????? failure : function(response,options){
??????????????????????????????????? Ext.Msg.alert("错误","数据加载失败!")
??????????????????????????????? }
??????????????????????????? });

alert 输出的结果为:resp:[object Object]/value:李四

第二种方式:
//后台返回值:
{"data":{"bookStatusId":20,"returnTime":"2011-03-05","bookSN":"123456-1","lendTime":"2011-03-02 11:20",
"borrower":"李四","bookId":61,"borrowerId":6,"lendOutId":34,"bookName":"1"}}

据说没有success : true , 是不能解析的。经过本人测试,这是错误的说法!
当我只改了后台(去掉了success),其他不变的前提下,输入结果跟第一种方式结果一此。故然反驳。

第三种方式:
//后台返回值去掉了最外层大括号,如下:
{ "bookStatusId":20,"bookSN":"123456-1","returnTime":"2011-03-05","lendTime":"2011-03-02 11:20",
"borrower":"李四","bookId":61,"borrowerId":6,"lendOutId":34,"bookName":"1"}

//前台解析:
?? success : function(response, options){
???????? var resp = Ext.decode(response.responseText);
???????? alert("resp:"+resp+"/value:"+resp .borrower);

?? }

alert 输出的结果为:resp:[object Object]/value:李四?

第四种方式:
//后台返回值:

[{ "bookStatusId":20,"bookSN":"123456-1","returnTime":"2011-03-05","lendTime":"2011-03-02 11:20",
"borrower":"李四","bookId":61,"borrowerId":6,"lendOutId":34,"bookName":"1"}]

//前台解析:
success : function(response, options){
? var resp = Ext.decode(response.responseText);

//? var resp = Ext.util.JSON.decode(response.responseText); //这种方式也行
? var a = resp[0].lendTime;
? alert("borrower:"+a);

}
alert 输出的结果为:borrower:李四。

这里也推翻了多数所说的接收数据规则,如:{ msg:"",data:""}

本人认为只要符合JSON数据接收形式就行了,再看你怎样去获取返回到前台的值。

?

差不多就这些了,多多交流,给点意见!!!不吝赐教!!!