在阅读JSON响应时遇到一些麻烦

时间:2013-02-28 01:19:13

标签: servlets request response extjs4.1

我向servlet发出一个ajax请求,返回数据库查询的结果。 我可以看到firebug的响应,我想把这些结果放在我已经创建的列表(或其他..)中。

我试着阅读this post,但它对我没有帮助..

代码:

这是ajax请求:

                        Ext.Ajax.request({
                        url: '/VIProject/Container',

                        success: function (action){alert('Lista caricata!'); console.debug(action); },
                        failure: function (){alert('Errore nel caricamento...');},
                        headers: {
                            'my-header': 'foo'
                        },
                        params: { action: "GETCONTAINERLIST" }
                    });
来自servlet(firebug)的响应:

{"message":"OK","container":[{"idOrdine":"1","numLotto":"123"},{"idOrdine":"2","numLotto":"321"},{"idOrdine":"3","numLotto":"876"}],"success":true}

列表:

    var listView = Ext.create('Ext.grid.Panel', {
    width:425,
    height:250,
    id: 'lista',
    collapsible:true,
    title:'Simple ListView <i>(0 items selected)</i>',
    store: //???
    multiSelect: true,
    viewConfig: {
        emptyText: 'No images to display'
    },

    columns: [{
        text: 'idOrdine',
        flex: 15,
        sortable: true,
        dataIndex: 'idOrdine'
    },{
        text: 'Last Modified',
        flex: 20,
        sortable: true,
        dataIndex: 'numLotto'
    }]
});

我该怎么办?

2 个答案:

答案 0 :(得分:0)

我已修改您的代码以使用商店:

Ext.define('MyModel', {
    extend: 'Ext.data.Model',
    fields: ['idOrdine', 'numLotto']
})

var listView = Ext.create('Ext.grid.Panel', {
    width:425,
    height:250,
    id: 'lista',
    collapsible:true,
    title:'Simple ListView <i>(0 items selected)</i>',
    store: store: {
        model: MyModel,
        autoLoad: true,
        proxy: {
            type: 'ajax',
            url: '/VIProject/Container',
            reader: {
                type: 'json',
                root: 'container'
            }
        }
    },
    multiSelect: true,
    viewConfig: {
        emptyText: 'No images to display'
    },

    columns: [{
        text: 'idOrdine',
        flex: 15,
        sortable: true,
        dataIndex: 'idOrdine'
    },{
        text: 'Last Modified',
        flex: 20,
        sortable: true,
        dataIndex: 'numLotto'
    }]
});

答案 1 :(得分:0)

即便以这种方式运作:

        var proxy=new Ext.data.HttpProxy({url:'/VIProject/Container'});

    var reader=new Ext.data.JsonReader({},[
          {name: 'idOrdine', mapping: 'idOrdine'},
          {name: 'numLotto', mapping: 'numLotto'}
     ]);

     var store=new Ext.data.Store(    {
         proxy:proxy,
         reader:reader
     });

    store.load();

我试着各方面......但我不知道该怎么办!