用JSON返回数据填充jqGrid并获取Uncaught TypeError无法读取属性'0'的未定义

Populating jqGrid with JSON return data and getting Uncaught TypeError Cannot read property '0' of undefined

本文关键字:读取 属性 未定义 TypeError 数据 返回 JSON 填充 jqGrid Uncaught 获取      更新时间:2023-09-26

我正试图使用Jackson从Spring Web应用程序返回一些JSON,并对其进行解析并将其加载到jqGrid中。

我已经通过JSONViewer扩展将数据返回并在Chrome中可见。在我看来是正确的。

使用arraydata进行的本地测试取得了成功。

这是我的JSP/HTML/JS:

 <link rel='stylesheet' type='text/css' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/ui-darkness/jquery-ui.css' />
 <link rel='stylesheet' type='text/css' href='css/jqGrid/ui.jqgrid.css' />
 <script type='text/javascript' src='js/jquery-1.7.1.min.js'></script>
 <script type='text/javascript' src='js/jquery-ui-1.8.17.custom.min.js'></script>
 <script type='text/javascript' src='js/i18n/grid.locale-en.js'></script>
 <script type='text/javascript' src='js/jquery.jqGrid.min.js'></script>
 <script type='text/javascript'>
$(document).ready(function () {
    jQuery("#list").jqGrid({        
        url:"formSubmit.html",
        datatype: "json",
        height: 700,
        width: 1100,
        colNames: ['ReqID', 'Family', 'ControlID', 'Name', 'Description', 'Category','Priority', 'Notes', 'Parent'],
        colModel: [
        { name: 'reqID', index: 'reqID', width: 40 },
        { name: 'family', index: 'family', width: 100 },
        { name: 'controlID', index: 'controlID', width: 100 },
        { name: 'reqName', index: 'reqName', width: 175 },
        { name: 'requirement', index: 'requirement', width: 450,cellattr: function (rowId, tv, rawObject, cm, rdata) { return 'style="white-space: normal;"' } },
        { name: 'category', index: 'category', width: 100 },
        { name: 'priority', index: 'priority', width: 100 },
        { name: 'requirementNotes', index: 'requirementNotes', width: 100 },
        { name: 'parent', index: 'parent', width: 100 }
        ],
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: '#pager',
        viewrecords: true,
        jsonReader : { repeatitems: false }
    });
    jQuery("#list").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false });
});
 </script>
<table id = 'list'></table>

以下是使用Jackson从Spring容器返回的一些数据:

{
"total":"1",
"page":"1",
"records":"558",
"rows":[{
    "parent":"",
    "priority":"",
    "requirementNotes":"DummyData",
    "category":"DummyData",
    "family":"DummyData",
    "requirement":"DummyData",
    "reqID":"1",
    "controlID":"DummyData",
    "reqName":"DummyData"}]
}

在jquery.jqGrid.min.js:23中,我一直在返回一个Uncaught TypeError。无法读取未定义中的属性"0"作为我的响应,我被难住了。

从使用您发布的JSON数据和代码的演示中可以看出,jqGrid应该可以正常工作。

我只能重复我在之前的评论中已经写过的内容:参数url:"formSubmit.html"在我看来非常可疑。如果你调用一些动态组件,你的URL应该没有像"/myurl/"这样的扩展名,或者有像".html"这样的其他扩展名。我建议你分析Fiddler、Firebug或IE或Chrome的开发工具的HTTP流量(查看"网络"选项卡)。重要的不仅可以是HTTP正文,还可以是像"内容类型"这样的HTTP标头。

尝试在jsonreader中设置root: "rows"