读书人

Ext.data.proxy 中jsonp方式获取数据

发布时间: 2012-12-15 15:16:03 作者: rapoo

Ext.data.proxy 中jsonp方式,获取数据的问题---初学者的问题
Model:
Ext.define('ecms.view.message.messageindexmodel', {
extend: 'Ext.data.Model',
fields: [
{ name: 'source', type:'string' },
{ name:'type',type:'string' },
{ name:'title', type:'string' },
{ name:'url',type:'string' }
]
});
Store:
Ext.define('ecms.view.message.messageindexstore', {
extend: 'Ext.data.Store',
model: 'ecms.view.message.messageindexmodel',
autoLoad: false,
remoteSort: true,
remoteFilter: true,
pageSize: 30,
autoSync: false,
sorters:[{
property:'source',
direction:'ASC'
}],
proxy: {
type: 'jsonp',
url : 'http://localhost:4529/Messagemg/jss.js',
reader: {
type: 'json',
implicitIncludes:true,
record : 'info'
}

}
});
调用:
var me = this;
me.store = Ext.create('ecms.view.message.messageindexstore');
me.store.load({
callback: function() {
var user = me.store.first();
alert(user);
}
});
JS:
var info={"info":[{"source":"1","type":"系统公告","title":"dddddddd (2012-01-26)","url":"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74"},{"source":"1","type":"系统公告","title":"dddddddd (2012-01-26)","url":"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74"}]}

跟踪请求已发出,就是读不出任何内容,显示为空
[最优解释]
在proxy的reader里没有定义root:


proxy: {
type: 'jsonp',
url : 'http://localhost:4529/Messagemg/jss.js',
reader: {
type: 'json',
implicitIncludes:true,
root : 'info' //感觉问题可能在这里,试试把你的record 换成root
}
}

[其他解释]
数据格式错了,看红色部分,还需要用Response.Write输出info
string cb = Request.Params.Get("cbackf");
string info = "{\"success\":true,\"total\":2,\"info\":{[{\"source\":\"1\",\"type\":\"系统公告\",\"title\":\"dddddddd (2012-01-26)\",\"url\":\"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74\"},{\"source\":\"1\",\"type\":\"系统公告\",\"title\":\"dddddddd (2012-01-26)\",\"url\":\"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74\"}]}}";
info = cb + "(" + info + ")";
Response.Write(info);
[其他解释]
参考http://www.sencha.com/forum/showthread.php?155122-Proxy-not-working-with-type-jsonp-!!!

把JSON输出放到一个ASPX中
String cb = Request.Params.Get("cbackf");
info = "{\"success\":true,\"total\":2,\"info\":{[{\"source\":\"1\",\"type\":\"系统公告\",\"title\":\"dddddddd (2012-01-26)\",\"url\":\"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74\"},{\"source\":\"1\",\"type\":\"系统公告\",\"title\":\"dddddddd (2012-01-26)\",\"url\":\"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74\"}]}}";
info = cb + "(" + info + ")";



把STORE改成:
proxy: {
type: 'jsonp',
url : 'http://localhost:4529/Messagemg/message_index.aspx',
callbackKey: 'cbackf',
reader : {
type : 'json',
root : 'info',
implicitIncludes:true,
totalCount : 'total'
}
可是问题依旧,问题出在那里呢?

josephSC:我修改后还是不行!
[其他解释]
是JSON格式不对,还是调用方法有问题,从网络监控可以看到请求发出,应答也回来了,理论上说问题是出在接收和JSON数据解析上!!!
[其他解释]
二楼给的连接的方法,是为了自己定义callback类的名字。也就是把默认的"callback"替换成了你现在用的"cbackf",所以要用到"callbackKey"。如果是默认的"callback"就不需要"callbackkey"。因为jsonP和其他请求不同,它是跨域的。所以必须在服务器端支持callback


你现在确定已经能够成功在网页得到完整的json?

如果是,那你的alert后跳出的对话框是空白还是“undefined"还是"Object [object Object] "?

你用的监控软件是firebug吧,如果是firebug有没有报其他错?

store.load的callback的格式为function(records, options, success)比如:


me.store.load({
callback: function(records, operation, success) {
alert(records); //试一下这样提示框里显示的是什么?
}
});



[其他解释]
showbo:加了{},ASPX输出为:
Ext.data.JsonP.callback1({"success":true,"total":2,"info":{[{"source":"1","type":"系统公告","title":"dddddddd (2012-01-26)","url":"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74"},{"source":"1","type":"系统公告","title":"dddddddd (2012-01-26)","url":"http://localhost:4529/Messagemg/message_edit_release.aspx?id=74"}]}})
问题依然。

josephSC:
你现在确定已经能够成功在网页得到完整的json?
是的

如果是,那你的alert后跳出的对话框是空白还是“undefined"还是"Object [object Object] "?
按你修改,ALRET为NULL
修改之前的ALERT为“undefined"

你用的监控软件是firebug吧,如果是firebug有没有报其他错?
是的,没有报其他错,一步步调试也没错

调用LOAD后,显示LOAD……很长时间后,callback回来,感觉就是请求没有得到响应的,但查看监控,请求是发出了,也给了响应。

???

[其他解释]
没有。。要删除{}...,你那样是错的,怪我没说完全
[其他解释]
呵呵,是啊,我说应该是不要{},但问题还像块石头一样纹丝不动
[其他解释]
现在可以了,按showbo说的修改JSON是对的,但之前还是不行,应该两个问题交叉在一起了,改READR定义,没改JSON,改了READR没改JSON,我重新写了一遍了后,现在可以了。

回思问题:
1、JSONP调用须在SERVER用回调函数调用JSON,不能直接返回JSON
2、JSON的格式一定要定义结点,以前一直是自定义解析所以没注意这个细节
3、josephSC说的ROOT一定要定义的
4、问题可能是多个交织在一起引起连锁返应,必要是还是重写比较容易整理思路

最后谢谢showbo和josephSC。

读书人网 >JavaScript

热点推荐