【问题标题】:jqgrid json data parsererrorjqgrid json数据解析器错误
【发布时间】:2011-07-11 03:53:12
【问题描述】:

我从服务器返回的json数据是这样的:

{
    "total": "3",
    "page": "1",
    "records": "21",
    "rows": [
        {
            "id": "6007",
            "cell": [
                "6007",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6008",
            "cell": [
                "6008",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6009",
            "cell": [
                "xxx",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6011",
            "cell": [
                "6011",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6012",
            "cell": [
                "6012",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6013",
            "cell": [
                "6013",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6014",
            "cell": [
                "6014",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6015",
            "cell": [
                "6015",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6016",
            "cell": [
                "6016",
                "xxx",
                "",
                "22.2"
            ]
        },
        {
            "id": "6017",
            "cell": [
                "6017",
                "xxx",
                "",
                "22.2"
            ]
        }
    ]
}

我在 jquery 就绪函数中使用了这个 jqGrid 声明:

 jQuery('#jqGrid1').jqGrid({
                caption: 'test grid',
                url: 'myurl',
                datatype: 'json',
                colNames: ["Column1", "Column2", "Weight", "NetWeight"],
                colModel: [
                    { "index": "Id", "width": 50, "name": "PartId" },
                    { "index": "Name","width": 100, "name": "CHNPartName" },
                    { "index": "Weight", "width": 75, "name": "Weight" },
                    { "index": "NetWeight", "name": "NetWeight"}
                ],
                viewrecords: true,
                loadError: jqGrid_aspnet_loadErrorHandler,
                pager:jQuery('#Pager1'),
                width: '640'
            });

但是在响应中出现解析器错误,任何人都可以帮助我吗?谢谢。

【问题讨论】:

  • 什么版本的jqgrid?什么版本的jquery?确切的错误信息是什么?
  • jqGrid 4.1.1,jquery 1.5.1。 loadError 函数中的状态是 'parsererror'
  • 我把 jquery 1.5.1 改成了 1.6.2,它工作正常,为什么?

标签: jquery json jqgrid parse-error


【解决方案1】:

我想您只是没有正确设置服务器响应的 HTTP 标头的Content-Type。如果是 JSON 数据,它应该是“application/json”,对应于RFC4627。如果您使用静态文件来保存文本数据,您可以尝试将文件扩展名重命名为“.json”。看the demo,它表明你的JSON数据可以被jQuery 1.6.2毫无问题地读取。

【讨论】:

  • 感谢@Oleg 的回复,我不使用静态文件。我可以在 Firebug 中看到服务器响应的 HTTP 标头的 Content-Type 是“application/json; charset=utf-8”。很困惑。
  • @Simon:“Content-Type”的值“application/json; charset=utf-8”也是正确的。如果您有在线测试页,我可以查看此内容并找到错误。从my demo 看到的问题不在于 JSON 数据本身,也不在于 jqGrid 设置。它必须在 HTTP 标头的级别上。通常,如果 Content-Type 与数据不对应,则 JSON 数据将被解析为 XML 数据,并且您会遇到解析错误。
  • 对不起,@Oleg ,没有在线测试页。在 jQuery 1.6.2 和 1.4.4 下一切正常,只是在 1.5.x 中出现这个错误。正如你上面所说,响应的标题应该没有问题。我认为,1.5.x和其他的ajax设置或json解析有什么区别吗?
  • @Simon:我不明白为什么你的演示应该在 jQuery 1.6.2 和 1.4.4 下工作,而不是在你的环境中使用 jQuery 1.5.x。我的演示版本在 jQuery 1.5.x 上运行没有任何问题。见here。因此,我认为您使用的数据与您发布的数据有所不同。不过,如果您可以毫无问题地使用 jQuery 1.6.2,我建议您使用它。
  • 顺便说一下,我使用的jstree插件也通过json获取数据,当我将ajax设置的'dataType'设置为'json'时,也会出现解析器错误,如果设置为'text json',它在 jQuery 1.5.x 下工作。为什么?可能是因为我的环境问题?@Oleg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-13
  • 1970-01-01
  • 2015-03-05
  • 2019-12-04
相关资源
最近更新 更多