0

这是我的 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}]}

但是当我尝试在浏览器中显示我的数据时,出现“无效标签”下方的错误

在此处输入图像描述

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

我的 json 数据必须类似于此链接,但我不知道如何构建我的数据,如下所示:http ://extjsinaction.com/dataQuery.php

4

2 回答 2

1

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

您的请求网址应该是这样的:

http://extjsinaction.com/dataQuery.php? 回调=回调函数

你的回复应该是这样的:

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 的信息,谷歌是你的朋友。

文档中的一些很好的例子

于 2012-06-01T11:10:15.707 回答
0
[{"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}]

你能从你的服务器发送这样的东西还是返回 JSONArray。我从来没有工作过,ExtJs但这就是我填充网格的方式DOJO

于 2012-06-01T10:47:11.287 回答