【问题标题】:jqgrid won't load json datajqgrid 不会加载 json 数据
【发布时间】:2012-03-22 14:57:01
【问题描述】:

我是 jqgrid 的新手,正在尝试使用 json 来加载数据。我尝试使用 jqgrid 演示作为基础,然后从那里构建。 json 数据看起来不错,但我无法将任何内容加载到网格中。有任何想法吗?我希望使用 loaderror 或 loadcomplete 至少可以让我了解情况,但我无法检索到为什么网格无法加载的任何消息。

json 数据:

{
    "page": "1",
    "total": 1,
    "records": "12",
    "rows": [
        [
            "67",
            "3 - Sandbox: Appointment Set"
        ],
        [
            "68",
            "1 - Sandbox: Email requested"
        ],
        [
            "69",
            "2 - Sandbox: Questions not answered"
        ],
        [
            "74",
            "1 - TenPointSix: Email requested for more information"
        ],
        [
            "75",
            "2 - TenPointSix: Registered for webinar2"
        ],
        [
            "76",
            "3 - TenPointSix: Webinar registration confirmed"
        ],
        [
            "93",
            "5-Test Entry"
        ],
        [
            "94",
            "test3"
        ],
        [
            "95",
            "test2"
        ],
        [
            "97",
            "Jeff"
        ],
        [
            "103",
            "sortorder"
        ],
        [
            "106",
            "reload"
        ]
    ]
}

我的网格代码:

<table id="jsonmap"></table>
<div id="pjmap"></div>       

    <script language="JavaScript" type="text/javascript">


 jQuery("#jsonmap").jqGrid({        
    url:'sampleLoad.php?client=<?=$clientId5?>',
    datatype: "json",
        ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    colNames:['Inv No','Name'],
    colModel:[
        {name:'id',index:'id', width:55},
        {name:'name',index:'name', width:100}

    ],
    rowNum:15,
    rowList:[15,30,45],
    pager: '#pjmap',
    sortname: 'id',

    viewrecords: true,
    sortorder: "asc",
    jsonReader: {
        root: "Rows",
        cell: "",
        page: "Page",
        total: "Total",
        records: "Records",
        repeatitems: false,
        id: "0"
    },
    loadComplete: function() {

        alert("Load Complete");
    },
    loadError: function(xhr,st,err) { $("#jsonmapMessage").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText); },



    caption: "JSON Mapping",
        width: '900',
                height: '300'

});


jQuery("#jsonmap").jqGrid('navGrid','#pjmap',{edit:true,add:false,del:false});

任何帮助将不胜感激。

谢谢,

杰夫

【问题讨论】:

    标签: json jqgrid jqgrid-php


    【解决方案1】:

    问题是您使用了错误的jsonReader。例如,您在 JSON 数据中使用 rows,但使用 root: "Rows"。数据格式对应默认repeatitems: true属性,但你使用repeatitems: false等等。

    正确的jsonReader

    jsonReader: {
        cell: "",
        id: "0"
    }
    

    另外,我建议您添加gridview: true 并使用height: 'auto' 而不是height: '300',这可以简化height 的设置。

    The demo 显示修改。

    【讨论】:

    • 奥列格,感谢您的帮助。我提出了你的建议,但仍然没有加载。还有其他想法吗? json阅读器:jsonReader:{单元格:“”,id:“0”},json:{“page”:“1”,“total”:1,“records”:“15”,“rows”:[[” 1","90 - 不是说话的好时机"],["3","94 - 无效号码"]]}
    • @Jeff:您应该更准确地比较您当前的代码与使用数据和代码the demo。我建议您另外实现loadError 回调,它将为您提供更多信息为什么网格为空(有关详细信息,请参阅the answer)。典型的问题是 HTTP 响应的错误 Content-type 标头。您可以使用。有关更多信息,请参阅 the answer 和 cmets。
    • 谢谢 - 我为我的错误消息遗漏了一个 div,并且我添加了内容类型的 json。最后,我发现我遇到了 json 解析错误 - 代码 200。我将开始搜索如何修复。谢谢你的帮助。杰夫
    • Oleg,我正在使用 php,我认为这不会产生任何影响,但我无法克服解析错误。我在 jsonlin 中检查了我的 json,一切都有效。我已经像您一样将 json 放入一个文件中,但是我的网格没有加载。我在回复中包含了内容类型和内容长度。我很困惑。 json: {"page":"1","total":1,"records":"15","rows":[["1","90 - 不是说话的好时机"]]} 我有你用来模仿我的网格的相同代码,除了我得到一个解析错误。还有其他想法吗?谢谢,杰夫
    • @Jeff:您写道:“我在回复中包含了内容类型和内容长度”。首先你不能设置内容长度。它将自动计算。其次,我认为您仍然有错误的“内容类型”。您不应该相信您使用的 PHP 代码。而不是 您应该验证 Content-type 确实具有 HTTP 响应的标头。您是否使用 FiddlerFirebug 或 Internet Explorer 或 Google Chrome 的开发者工具的“网络”选项卡跟踪 HTTP 流量?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    相关资源
    最近更新 更多