【问题标题】:JqGrid not loading data from JSONJqG​​rid 不从 JSON 加载数据
【发布时间】:2011-03-11 12:57:38
【问题描述】:

我的网站中有一个 JQGrid 插件,表格加载正常,但没有行可编辑、选择或其他。它正在请求服务器,因为当我查看日志时,我看到了请求及其参数(sidx、_search、rows、sord、nd 等)。

这是jqgrid代码:

$(document).ready(function() {
    var lastsel;
    jQuery("#rowed3").jqGrid(
            {
                url : CONTEXT_PATH+'/ajax/getPartesByCategory.do?catid=<s:property value="categoryId" />',
                datatype: 'json',
                colNames : [ 'piezaId', 'descripcion', 'disponible'],
                colModel : [ {
                    name : 'piezaId',
                    index : 'piezaId',
                    align : "right",
                    width : 40
                }, {
                    name : 'descripcion',
                    index : 'descripcion',
                    width : 360,
                    editable : true
                }, {
                    name : 'disponible',
                    index : 'disponible',
                    width : 80,
                    editable : true
                } ],
                rowNum : 20,
                rowList : [ 20, 40, 60, 80 ],
                pager : '#prowed3',
                sortname : 'id',
                viewrecords : true,
                sortorder : "desc",
                onSelectRow : function(id) {
                    if (id && id !== lastsel) {
                        jQuery('#rowed3').jqGrid('restoreRow', lastsel);
                        jQuery('#rowed3').jqGrid('editRow', id, true);
                        lastsel = id;
                    }
                },
                editurl : "server.php",
                caption : "Piezas"
            });
    jQuery("#rowed3").jqGrid('navGrid', "#prowed3", {
        edit : false,
        add : false,
        del : false
    });
})

这是服务器返回的 JSON:

[{
  "piezaId": 486,
  "disponible": 1,
  "descripcion": "Asiento delantero LH",
  "category": {
    "categoryId": 2,
    "category": "Interior",
    "status": 1,
    "subCategories": []
  }
}, {
  "piezaId": 485,
  "disponible": 1,
  "descripcion": "Asiento delantero RH",
  "category": {
    "categoryId": 2,
    "category": "Interior",
    "status": 1,
    "subCategories": []
  }
}]

在服务器端,我使用 JAVA6、Struts2 和 GSon 编写 json,但对于这些 ajax 调用,我只向响应写入文本/纯文本内容

有什么帮助吗?

【问题讨论】:

  • CONTEXT_PATH 是否与此页面在同一个域中?另外,你为什么不使用application/json 来回复?
  • 是的,CONTEXT_PATH 在同一个域中,它在另一个首先加载的 .js 中(从模板中)...为什么我不使用 application/json,不知道只是开始编码并且不知道应用程序/json。无论如何,我刚刚实现了使用 application/json 的测试,并且发生了同样的情况......
  • JSON 不应该是 {"piezaId":486, ... } 换句话说,没有方括号:[ ]。
  • @George,JSON 有效。这是一个包含两个对象的数组。但是,将数组作为最外层类型确实支持某些攻击。请参阅 [是否可以对返回 JSON 对象的 URL 执行跨站点请求伪造攻击? ](stackoverflow.com/questions/3170196/…)。
  • @Matthew OIC 现在。 JQGrid 是否需要 application/json 的内容类型?

标签: java javascript jquery json jqgrid


【解决方案1】:

您应该更改从服务器发送的数据格式或使用一些jsonReaderjsonmap 技术(参见例如jquery with ASP.NET MVC - calling ajax enabled web servicehttp://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_dataMapping JSON data in JQGrid)。最简单的方法是生成可以用标准jsonReader 读取的数据(参见http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data):

{
  "total": "1", 
  "page": "1", 
  "records": "2",
  "rows" : [
    {"id" :"1", "cell":["486","1","Asiento delantero LH"]},
    {"id" :"2", "cell":["485","1","Asiento delantero RH"]},
  ]
}

【讨论】:

  • 欢迎您!使用jsonReader 尤其是您可以实际读取的功能和JSON 数据。我喜欢这个功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-01
相关资源
最近更新 更多