【问题标题】:ExtJS RESTful service fail to handle json dataExtJS RESTful 服务无法处理 json 数据
【发布时间】:2016-03-05 05:40:17
【问题描述】:

我实现了简单的 RESTful 服务。这些是 Rest 请求 url 和响应 json 数据。

http://localhost:8080/RESTfulTestWeb/rest/services/getJson/aupres

{"id":"aupres","passwd":"aaa","age":45,"name":"joseph"}

问题是ExtJS Store对象客户端无法处理上述json数据。这些是 ExtJS 客户端代码

Ext.define('extjs.model.Member', {
    extend: 'Ext.data.Model',

    fields : [{
        name : 'id',
        type : 'string'
    }, {
        name : 'passwd',
        type : 'string'
    }, {
        name : 'age',
        type : 'int'
    }, {
        name : 'name',
        type : 'string'
    }]  
});

Ext.onReady(function () {

    var members = Ext.create('Ext.data.Store', {

        model : 'extjs.model.Member',
        //autoLoad : true,
        proxy: {
            type: 'rest',
            url : 'http://localhost:8080/RESTfulTestWeb/rest/services/getJson/aupres',
            reader: {
                type: 'json',
                root: 'id',
            },
            writer: {
                type: 'json'
            },
            listeners: {
                exception: function(proxy, response, operation){
                    Ext.MessageBox.show({
                        title: 'REMOTE EXCEPTION',
                        msg: operation.getError(),
                        icon: Ext.MessageBox.ERROR,
                        buttons: Ext.Msg.OK
                    })
                }
            }

        },
        listeners: {
            load: function(members, operation, success) {
                if(success) {
                    alert('response : ' + members.model.length)
                } else {
                    alert('it failed')
                }
            }
        }
    })

    var onButtonClick = function() {
        members.load()
    }

响应如下所示

"response : 0"

我的 ExtJS 代码似乎无法包含 json 数据。

【问题讨论】:

  • 您是否在没有readerwriter 配置的情况下进行了测试? (建议:一定要在模型中配置代理,因为代理和模型是绑定的)

标签: javascript json rest extjs5


【解决方案1】:

ExtJs 5 及更高版本中的其余代理阅读器查找 rootProperty: 而不是 root:

【讨论】:

  • 您也不想使用 'id' 的 rootProperty - 它希望在那里找到一个可以变成模型的对象。鉴于上面显示的 JSON,您根本不需要 rootProperty。
【解决方案2】:

你可以覆盖你的类的 toString() 方法。那么你可以得到准确的。

就像你想要关于 ["id":"aupres","passwd":"aaa","age":45,"name":"joseph"}] 的数据 或其他格式

【讨论】:

  • 恐怕我是 javascript 的新手,所以我不知道如何覆盖 toString 方法,请告诉我示例代码或参考。地点。谢谢
  • toString() 你必须从服务器端覆盖。在 java 代码中你必须覆盖而不是在你的客户端代码中
猜你喜欢
  • 1970-01-01
  • 2016-09-28
  • 2016-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多