这是我的ExtJS代码,用于显示网格中的记录。

   var remoteProxy = new Ext.data.ScriptTagProxy({
                url : 'hastanebilgilerinigetir'
            });

            var recordFields = [
                                { name : 'hid',          mapping : 'hid'},
                                { name : 'isim',   mapping : 'isim'},
                                { name : 'adres',    mapping : 'adres'},
                                { name : 'telefon',      mapping : 'telefon'},
                                { name : 'bashekimid',        mapping : 'bashekimid' }
            ];

            var remoteJsonStore = new Ext.data.JsonStore({
                proxy           : remoteProxy,
                storeId         : 'ourRemoteStore',
                root            : 'records',
                autoLoad        : false,
                //totalProperty   : 'totalCount',
                remoteSort      : true,
                fields          : recordFields,
                idProperty      : 'hid'
            });

            var textFieldEditor = new Ext.form.TextField();

            var numberFieldEditor = {
                    xtype     : 'numberfield',
                    minLength : 5,
                    maxLength : 5
            };

            var columnModel = [
                               {
                                   header    : 'Hastane Adı',
                                   dataIndex : 'isim',
                                   sortable  : true,
                                   editor    : textFieldEditor
                               },
                               {
                                   header    : 'Telefon',
                                   dataIndex : 'telefon',
                                   sortable  : true,
                                   editor    : textFieldEditor
                               },
                               {
                                   header    : 'Adres',
                                   dataIndex : 'adres',
                                   sortable  : true,
                                   editor    : textFieldEditor
                               },
                               {
                                   header    : 'Baş Hekim ID',
                                   dataIndex : 'bashekimid',
                                   sortable  : true,
                                   editor    : numberFieldEditor
                               },
                               {
                                   header    : 'Hastane ID',
                                   dataIndex : 'hid',
                                   sortable  : true,
                                   editor    : numberFieldEditor
                               }
                               ];
    var grid = {
            xtype      : 'editorgrid',
            columns    : columnModel,
            id         : 'myEditorGrid',
            store      : remoteJsonStore,
            loadMask   : true,
            bbar       : pagingToolbar,
            stripeRows : true,
            viewConfig : {
                forceFit : true
            },
            listeners        : {
                cellcontextmenu : doCellCtxMenu,
                destroy         : function(thisGrid) {
                    if (thisGrid.rowCtxMenu) {
                        thisGrid.rowCtxMenu.destroy();
                    }
                }
            }
    };

    var hastanegridi = new Ext.Panel({
        title: 'Hastaneler',
        height  : 700,
        width   : 950,
        border  : false,
        layout  : 'fit',
        items   : grid
    });


    remoteJsonStore.load({
        params : {
            start : 0,
            limit : 50
        }
    });


我有以下Java代码来创建JSONObject

    @Override
    public JSONObject listHastanesAsJson() {
        List<Hastane> lst = hastaneDao.listHastane();

        JSONArray arr = new JSONArray();

//      int i=lst.size();
//      JSONObject counter = new JSONObject();
//      counter.put("totalCount", i);
//      arr.add(counter);


        for(Hastane kan:lst){

            JSONObject obj = new JSONObject();

            obj.put("hid", kan.getHid());
            obj.put("isim", kan.getIsim());
            obj.put("adres", kan.getAdres());
            obj.put("telefon", kan.getTelefon());
            obj.put("bashekimid", kan.getBahekimid());
            arr.add(obj);
        }


        JSONObject o = new JSONObject();
        o.put("records", arr);
        return o;
    }


我的Json数据如下:

{"records":[{"hid":1,"isim":"Zeynep Kamil","adres":"Istanbul","telefon":"056765434567","bashekimid":1},{"hid":2,"isim":"Ankara Hastanesi","adres":"Ankara","telefon":"345678987658","bashekimid":2},{"hid":3,"isim":"Baskent Hastanesi","adres":"Ankara","telefon":"567898765697","bashekimid":3}]}


但是,当我尝试在浏览器中显示数据时,在“İnvalidlabel”下方出现错误



在服务器端创建JSONObject时是否出错?

我的json数据必须类似于此链接中的内容,但我不知道如何构建这样的数据:http://extjsinaction.com/dataQuery.php

最佳答案

您正在使用Ext.data.ScriptTagProxy巫婆用于JSONP(跨域请求)

您的请求网址应如下所示:

http://extjsinaction.com/dataQuery.php?callback=callbackFunction

您的回答应该是这样的:

callbackFunction({
    "records": [
        {
            "hid": 1,
            "isim": "Zeynep Kamil",
            "adres": "Istanbul",
            "telefon": "056765434567",
            "bashekimid": 1
        },
        {
            "hid": 2,
            "isim": "Ankara Hastanesi",
            "adres": "Ankara",
            "telefon": "345678987658",
            "bashekimid": 2
        },
        {
            "hid": 3,
            "isim": "Baskent Hastanesi",
            "adres": "Ankara",
            "telefon": "567898765697",
            "bashekimid": 3
        }
    ]
})


如果您需要有关JSONP的更多信息,则google是您的朋友。

一些很好的examples in documentation

10-08 01:21