我从WebService获得Json响应,如下所示
Json完美地返回数据。数据看起来像
{" d":" [{\" ID \":2,\"代码\":\" mycode的\" \"名称\":\"的Myname \" \&#34,密码\":\" A \ " \" ClientLevel \":0,\" DEPTNO \":\" \" \" DEPTNAME \& #34;:\" \"},{\" ID \":3,\"代码\":\" mycode的\ " \"名称\":\" LY1 \" \&#34,密码\":\"输入mypassword \&# 34;,.......但不能绑定我的jqgrid。
我有以下Jqgrid代码
jQuery("#list2").jqGrid({
mtype: 'POST',
url: "myservice.asmx/GetQueryInfo",
serializeGridData: function (postData) {
return JSON.stringify({
TableNames: TableName,
ColumnList: ColumnNames
});
},
ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
jsonReader: {
repeatitems: false,
root: 'd',
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.toString().length; }
},
datatype: 'json',
colNames: ['ID', 'Code', 'Name', 'PassWord', 'ClientLevel', 'DeptNo', 'DeptName'],
colModel: [
{ name: "ID", width: 55 },
{ name: "Code", width: 90 },
{ name: "Name", width: 100 },
{ name: "PassWord", width: 80 },
{ name: "ClientLevel", width: 80 },
{ name: "DeptNo", width: 80 },
{ name: "DeptName", width: 150 }
],
autoencode: true,
gridview: true,
rowNum: 10,
loadonce: true,
rowList: [10, 20, 30],
pager: '#pager2',
sortname: 'ID',
viewrecords: true,
sortorder: "ID",
caption: "JSON Example",
loadError: function (jqXHR, textStatus, errorThrown) {
alert('HTTP status code: ' + jqXHR.status + '\n' +
'textStatus: ' + textStatus + '\n' +
'errorThrown: ' + errorThrown);
alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
}
});
jQuery("#list2").jqGrid('navGrid', '#pager2', { edit: false, add: false, del: false });
答案 0 :(得分:1)
我仍然认为您在服务器端使用旧代码(WebMethod
返回字符串而不是对象),因为 d 属性的值是字符串。您可以在jsonReader
中使用root作为the answer中定义的函数。在这种情况下,它也适用于您的情况。所以你可以使用
jsonReader: {
repeatitems: false,
root: function (obj) {
return typeof obj.d === "string" ? $.parseJSON(obj.d) : obj.d;
}
}