【问题标题】:ExtJS JsonStore and NettyExtJS JsonStore 和 Netty
【发布时间】:2011-06-20 01:01:57
【问题描述】:

我试图阻止 ExtJS 和 Java 之间的通信我正在将请求从 ExtJS 发送到使用 netty 的 Java 服务器。如果有人可以向我发送一个示例,说明如何从 java 端格式化响应以及如何从 ExtJS 端读取响应数据,我会提前感谢。 这是我在 ExtJS 方面的来源

var store = new Ext.data.JsonStore({
    autoload: true,
    baseParams: {
        conid : '6b09477f-aa04-4f5a-b969-05277d01f07e'
    },
    root: 'OpenCashTime',
    proxy: new Ext.data.ScriptTagProxy({
        url: 'http://localhost:8080/getOpenCash?'
    }),
    fields: [{name: 'Time', mapping: 'Time', type: 'int'}]                  
});
store.load();
store.on('load', function() {
    alert(store.getTotalCount());                   
});
store.on('write', function() {                  
    alert(store.getTotalCount());
});
store.on('loadexception', function() {
    alert("AAAAAA");
});
store.on('metachange', function() {
    //alert(store.getTotalCount());
});
store.on('update', function() {
    //alert(store.getTotalCount());
});
store.on('save', function() {
    //alert(store.getTotalCount());
});
store.on('datachanged', function() {
    //alert(store.getTotalCount());
});

执行此代码并接收此响应时 {"OpenCashTime":[{"Time":1291623637000},{"Time":1294914317000}]} 尽管即使 firebug 看到它的 Json,我仍然收到 loadexception

【问题讨论】:

  • 为什么选择 ScripTagProxy?你会从跨域源加载数据吗?
  • 是的,现在我在 localhost 上进行测试,而且它不在 ExtJS 在 Apache Tomcat 服务器上运行的同一台服务器上,netty 部分在不同的端口上运行,因此它被视为跨域
  • 好的。对于 ScriptTagProxy,您的响应必须如下所示:callback({"OpenCashTime": [{"Time":1291623637000},{"Time":1294914317000}]})
  • 那我不应该加个回调函数名吗?
  • 是的。实际上,您应该使用callbackParam 参数配置您的ScriptTagProxy,即The name of the parameter to pass to the server which tells the server the name of the callback function set up by the load call to process the returned data object. Defaults to "callback". 虽然我似乎记得,callback 是使用的默认值。

标签: java extjs netty


【解决方案1】:

根据您的标题,假设您希望将数据加载到 JsonStore,它需要一个有效的 Json 字符串,该字符串具有一个存储 JSON 对象数组的属性,该字符串将作为记录加载。属性名由配置JsonStore时root属性设置。

这样存储:

{
  xtype: 'jsonstore',
  root: 'data',
  idProperty: 'ID',
  fields: [
    {name: 'ID', mapping: 'ID', type: 'int'}
    {name: 'someDate', mapping: 'someDate', type: 'date', dateFormat: 'Y-m-d'}
  ],
  url: 'hereBeYourURl'
}

会很乐意吃这样的东西:

{"data":[{"ID":"1","someDate":"2002-10-02"},{"ID":"2","someDate":"2002-05-22"}]}

【讨论】:

  • 我在回复中得到的字符串是 OpenCashTime{Time:1291623637000,Time:1294914317000} 所以我想我应该写 root: 'OpenCashTime' 并在字段中: {name: 'Time' , mapping: 'Time', type: 'int'} 或者我应该以某种方式更改字符串以适应当前格式?
  • 对于Ext.JsonStore,您需要一个有效的 JSON 字符串。您现在拥有的远非有效的 JSON。你需要这样的东西:{"OpenCashTime": [{"Time":1291623637000},{"Time":1294914317000}]}
  • 虽然我的字符串现在没问题,但我仍然收到错误消息,我将编辑我的帖子并添加我的 ExtJS 源代码
  • @Swine1973:你为什么使用 ScriptTagProxy?你打算从跨域源加载这些数据吗?如果是,那么它的格式需要稍微不同。如果没有,请使用 JsonStore 中的默认代理。
【解决方案2】:
fields: [{name: 'Time', mapping: 'Time', type: 'int'}]  
fields: [{name: 'Time', type: 'int'}]  

顺便说一句,对于身份映射,您可以将其省略。这两种情况会给你 同样的结果。

【讨论】:

  • 对不起,它对我有帮助吗?如果你知道这部分代码是正确的?
  • 我喜欢明确设置映射。使更改更容易。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-17
  • 2012-11-03
  • 1970-01-01
  • 2011-11-11
  • 2011-09-04
  • 1970-01-01
  • 2011-02-20
相关资源
最近更新 更多