Jaap Terlouw
Jaap Terlouw

Reputation: 125

jqGrid does not load and with error that its invalid xml, however data is JSON

I get onloading the jqGrid a error as follow:

load error: Error: Invalid XML: {"d":[{"id":1,"name":"Medical 1","city":"Kiev","instituteTypeId":0},{"id":2,"name":"Medical 2","city":"Kherson","instituteTypeId":0}]}

however I use JSON, oleg advised me to open new threat.

The jquery code is:

  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);
                },

There is no dataType= xml or anything defined....

Upvotes: 0

Views: 1028

Answers (1)

Oleg
Oleg

Reputation: 221997

You use dataType: 'json' which is wrong. jqGrid has the option datatype and no dataType. So you should use dataType: 'json'. Unknown option dataType will be just ignored and default option dataType: 'xml' will be used.

Additionally I think you should use just jsonReader: { root: "d" }.

The demo should be close to what you need. So you should do something like

$("#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" }
    ...
});

Upvotes: 3

Related Questions