首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JSON值填充ext网格时出现问题

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

Stack Overflow用户
提问于 2011-06-04 17:12:41
回答 2查看 1.8K关注 0票数 1

我是Ext的新手,我有一个问题:我正在尝试用数据填充extjs-grid:

代码语言:javascript
复制
    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部分:

代码语言:javascript
复制
@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错误:

代码语言:javascript
复制
Syntax error at line 1 while loading:
totalCount:'3',topics:[{title:'XTemplate
---------------------^
expected ';', got ':'

不过,当我使用Proxy的URL时:

代码语言:javascript
复制
URL: 'http://extjs.com/forum/topics-browse-remote.php',

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

我的失败在哪里?

附注:第一个答案的注释:

代码语言:javascript
复制
    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\"}]}";

我得到了以下错误:

代码语言:javascript
复制
Syntax error at line 1 while loading:
{"totalCount":"3","topics":[{"title
-------------^
expected ';', got ':'

附注#2.当我在响应字符串的开头添加了'‘,在结尾添加了'’时,错误消失了,但是网格还没有填满数据

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-10 22:14:15

我找到了问题的根源。

据我所知,Ext send通过参数'callback=some_callback_name‘(例如callback1001)发送到我的web服务函数。这意味着Extjs不仅希望得到JSON格式的结果,还希望得到'callback1001()‘格式的结果。当我以这种格式返回数据时,一切都变得很好。

证明链接:

  • http://www.sencha.com/forum/showthread.php?22990-Json-Invalid-label (响应#6)
  • http://indiandeve.wordpress.com/2009/12/02/extjs-error-invalid-label-error-while-using-scripttagproxy-for-json-data-in-paging-grid-example/
票数 0
EN

Stack Overflow用户

发布于 2011-06-04 17:14:38

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

例如,这是而不是有效的JSON:

代码语言:javascript
复制
{totalCount:'3'}

...because键不在引号中,值使用单引号。正确的JSON应该是:

代码语言:javascript
复制
{"totalCount":"3"}

...if您确实希望3是一个字符串,或者:

代码语言:javascript
复制
{"totalCount":3}

...if 3应该是一个数字。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6235849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档