使用JSON值填充ext网格时出现问题

时间:2011-06-04 09:12:41

标签: java json extjs grid

我是Ext的新用户,我遇到了一个问题:我正在尝试用数据填充extjs-grid:

    Ext.onReady(function() {
        var store = new Ext.data.JsonStore({
            root: 'topics',
            totalProperty: 'totalCount',
            idProperty: 'threadid',
            remoteSort: true,
            autoLoad: true, ///

            fields: [
                'title', 'forumtitle', 'forumid', 'author',
                {name: 'replycount', type: 'int'},
                {name: 'lastpost', mapping: 'lastpost', type: 'date', dateFormat: 'timestamp'},
                'lastposter', 'excerpt'
            ],

            proxy: new Ext.data.ScriptTagProxy({
            url:'http://10.10.10.101:8080/myproject/statusList/getJobs/2-10/search-jobname-/sort-asdf/filterjobname-123/filterusername-davs/filterstatus-completed/filtersubmdate-today',
            method : 'GET'
        })
    });

    //
    var cm = new Ext.grid.ColumnModel([
        {sortable:true, id : 'id',          dataIndex:'id'},
        {sortable:true, id : 'title',       dataIndex:'title'},
        {sortable:true, id : 'forumtitle',  dataIndex:'forumtitle'},
        {sortable:true, id : 'forumid',     dataIndex:'forumid'},
        {sortable:true, id : 'author',      dataIndex:'author'}
    ]);

    var grid = new Ext.grid.GridPanel({
        id: 'mainGrid',
        el:'mainPageGrid',
        pageSize:10,
        store:store,
        //    stripeRows: true,
        cm:cm,
        stateful: false, // skipSavingSortState
        viewConfig:{
            forceFit:true
        },
        //        width:1000,
        //        height:700,
        loadMask:true,
        frame:false,

        bbar: new Ext.PagingToolbar({
            id : 'mainGridPaginator',
            store:store,
            hideRefresh : true,
            plugins: new Ext.ux.Andrie.pPageSize({
                beforeText: 'View: ',
                afterText: '',
                addAfter: '-',
                variations: [10, 25, 50, 100, 1000]
                //comboCfg: {
                    //id: '${ dispview_widgetId }_bbar_pageSize'
                //}
            }),
            displayMsg: 'Displaying items {0} - {1} of {2}',
            emptyMsg:'No data found',
            displayInfo:true
        })
    });

    grid.render();
});

和Java部分:

@GET
@Path("/getJobs/{startFrom}-{startTo}/search-{searchType}-{searchName:.*}/" +
        "sort-{sortType}/filterjobname-{filterJobName:.*}/filterusername-{filterUsername:.*}/" +
        "filterstatus-{filterStatus:.*}/filtersubmdate-{filterSubmittedDate:.*}")
@Produces({"application/json"})
@Encoded
public String getJobs(
        @PathParam("startFrom")             String startFrom,
        @PathParam("startTo")               String startTo,
        @PathParam("searchType")            String searchType,
        @PathParam("searchName")            String searchName,
        @PathParam("sortType")              String sortType,
        @PathParam("filterJobName")         String filterJobName,
        @PathParam("filterUsername")        String filterUsername,
        @PathParam("filterStatus")          String filterStatus,
        @PathParam("filterSubmittedDate")   String filterSubmittedDate) {


    return "{totalCount:'3',topics:[{title:'XTemplate with in EditorGridPanel',threadid:'133690',username:'kpremco',userid:'272497',dateline:'1305604761',postid:'602876',forumtitle:'Ext 3x Help',forumid:'40',replycount:'2',lastpost:'1305857807',lastposter:'kpremco',excerpt:'Hi I have an EditiorGridPanel whose one column i am using XTemplate to render and another Column is Combo Box FieldWhen i render the EditorGri'}," +
                                             "{title:'IFrame error _flyweights is undefined',threadid:'133571',username:'Daz',userid:'52119',dateline:'1305533577',postid:'602456',forumtitle:'Ext 3x Help',forumid:'40',replycount:'1',lastpost:'1305857313',lastposter:'Daz',excerpt:'For Ext 330 using Firefox 4  Firebug, the following error is often happening when our app loads e._flyweights is undefined Yetthis '}," +
                                             "{title:'hellllllllllllllpwhy it doesnt fire cellclick event after I change the cell value',threadid:'133827',username:'aimer311',userid:'162000',dateline:'1305700219',postid:'603309',forumtitle:'Ext 3x Help',forumid:'40',replycount:'3',lastpost:'1305856996',lastposter:'aimer311',excerpt:'okI will discribe this problem as more detail as I canI look into this problem for a whole dayI set clicksToEdit1 to a EditorGridPanelso when I'}]}";

结果我收到了一个JavaScript错误:

Syntax error at line 1 while loading:
totalCount:'3',topics:[{title:'XTemplate
---------------------^
expected ';', got ':'

虽然,当我使用代理的URL时:

URL: 'http://extjs.com/forum/topics-browse-remote.php',

代表相同的信息,我没有任何问题。

我的失败在哪里????

P.S。第一个答案的评论:

    return "{\"totalCount\":\"3\",\"topics\":[{\"title\":\"XTemplate with in EditorGridPanel\",\"threadid\":\"133690\",\"username\":\"kpremco\",\"userid\":\"272497\",\"dateline\":\"1305604761\",\"postid\":\"602876\",\"forumtitle\":\"Ext 3x Help\",\"forumid\":\"40\",\"replycount\":\"2\",\"lastpost\":\"1305857807\",\"lastposter\":\"kpremco\",\"excerpt\":\"Hi I have an EditiorGridPanel whose one column i am using XTemplate to render and another Column is Combo Box FieldWhen i render the EditorGri\"}," +
                                             "{\"title\":\"IFrame error _flyweights is undefined\",\"threadid\":\"133571\",\"username\":\"Daz\",\"userid\":\"52119\",\"dateline\":\"1305533577\",\"postid\":\"602456\",\"forumtitle\":\"Ext 3x Help\",\"forumid\":\"40\",\"replycount\":\"1\",\"lastpost\":\"1305857313\",\"lastposter\":\"Daz\",\"excerpt\":\"For Ext 330 using Firefox 4  Firebug, the following error is often happening when our app loads e._flyweights is undefined Yet, this \"}," +
                                             "{\"title\":\"hellllllllllllllpwhy it doesn't fire cellclick event after I change the cell value\",\"threadid\":\"133827\",\"username\":\"aimer311\",\"userid\":\"162000\",\"dateline\":\"1305700219\",\"postid\":\"603309\",\"forumtitle\":\"Ext 3x Help\",\"forumid\":\"40\",\"replycount\":\"3\",\"lastpost\":\"1305856996\",\"lastposter\":\"aimer311\",\"excerpt\":\"okI will discribe this problem as more detail as I canI look into this problem for a whole dayI set clicksToEdit1 to a EditorGridPanelso when I\"}]}";

我遇到以下错误:

Syntax error at line 1 while loading:
{"totalCount":"3","topics":[{"title
-------------^
expected ';', got ':'

P.S。 #2。当我将'['添加到响应字符串的开头并且']'添加到结尾时,错误就消失了,但是网格还没有填充数据

2 个答案:

答案 0 :(得分:2)

你没有返回(有效)JSON。有关详细信息,请参阅JSON site,但是,例如,所有属性键必须使用双引号。 (所有字符串也必须是双引号;单引号对JSON字符串无效。)

例如,这是有效的JSON:

{totalCount:'3'}

...因为键不在引号中,并且值使用单引号。正确的JSON将是:

{"totalCount":"3"}

...如果您真的希望3成为字符串,或者:

{"totalCount":3}

...如果3应为数字。

人们经常混淆JSON和JavaScript的对象文字表示法,但它们是不同的。具体来说,JSON是对象文字表示法的子集。在对象文字符号中有效的许多内容在JSON中无效。如果您有任何疑问,可以查看jsonlint.com,它提供了一个合适的JSON验证器。

答案 1 :(得分:0)

我找到了问题的根源。

正如我所知,Ext使用参数'callback = [some_callback_name]'(例如callback1001)发送到我的Web服务功能。这意味着Extjs想要获得的结果不仅仅是JSON格式,而是格式'callback1001()'。当我以这种格式返回我的数据时,一切都变得很好。

证明链接: