【问题标题】:jqGrid does not load and with error that its invalid xml, however data is JSONjqGrid 未加载,错误提示其 xml 无效,但数据为 JSON
【发布时间】:2013-10-11 17:18:12
【问题描述】:

我在加载 jqGrid 时出现如下错误:

加载错误:错误:无效 XML:{"d":[{"id":1,"name":"Medical 1","city":"Kiev","instituteTypeId":0},{"id":2,"name":"医疗 2","city":"赫尔松","instituteTypeId":0}]}

无论我使用 JSON,oleg 建议我打开新威胁。

jquery代码为:

  mtype: 'POST',
  contentType: "application/json",
  url: "dataServices/objects.asmx/InvokeData",
  ajaxGridOptions: {
      contentType: 'application/json; charset=utf-8'
  },
     postData: JSON.stringify({q: "med&1"}),
                loadonce: true,
                dataType: 'json',
                jsonReader: {
                    root: function (obj) {
                        alert(obj.d);
                        return obj.d;
                    },
                    page: "",
                    total: "",
                    records: function (obj) {
                        return obj.d.length;
                    },
                },
                gridview: true,
                loadError: function (xhr, status, error) {
                    alert('load error: ' + error);
                },

没有 dataType= xml 或任何定义....

【问题讨论】:

    标签: ajax json jqgrid


    【解决方案1】:

    您使用dataType: 'json' 这是错误的。 jqGrid 有datatype 选项,没有dataType。所以你应该使用dataType: 'json'。未知选项 dataType 将被忽略,默认选项 dataType: 'xml' 将被使用。

    另外我认为你应该只使用jsonReader: { root: "d" }

    The demo 应该接近您的需要。所以你应该做类似的事情

    $("#list").jqGrid({
        mtype: 'POST',
        url: "dataServices/objects.asmx/InvokeData",
        datatype: "json",
        ajaxGridOptions: {
            contentType: "application/json; charset=utf-8"
        },
        postData: JSON.stringify({q: "med&1"}),
        loadonce: true,
        jsonReader: { root: "d" }
        ...
    });
    

    【讨论】:

    • 非常感谢您的帮助,如果您在基辅,请告诉我,我们会邀请您度过一个愉快的夜晚)))
    • @JaapTerlouw:不客气!我很高兴能帮助你。到目前为止,我从未去过基辅,但感谢您的邀请。
    猜你喜欢
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多