这是JSON:
{
"page": "1",
"total": "1",
"records": "2",
"rows": [
{
"Id": 3,
"FirstName": "Test",
"LastName": "Testerson",
"CustomerNumber": "1234",
"EmailAddress": "test@test.com",
"Subject": "Test1",
"OrderNumber": "4321",
"Comments": "This is a test"
},
{
"Id": 4,
"FirstName": "Test2",
"LastName": "Testerson2",
"CustomerNumber": "2222",
"EmailAddress": "test@test.com",
"Subject": "Test1",
"OrderNumber": "3333",
"Comments": "This is a test"
}
]
}
这是我当前的脚本和标记(为什么这不起作用?):
<table id="list1"></table>
<div id="pager1"></div>
<script type="text/javascript">
function getData(pdata) {
var params = new Object();
params.page = pdata.page;
params.pageSize = pdata.rows;
params.sortIndex = pdata.sidx;
params.sortDirection = pdata.sord;
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "ScriptServices/PNScriptService.asmx/GetContactRequests",
data: JSON.stringify(params),
dataType: "json",
success: function (data, textStatus) {
if (textStatus == "success") {
var thegrid = $("#list1")[0];
thegrid.addJSONData(data.d);
}
},
error: function (data, textStatus) {
alert('An error has occured retrieving the data.');
}
});
}
$(document).ready(function () {
$("#list1").jqGrid({
datatype: function (pdata) {
getData(pdata);
},
jsonReader: {
page: "page",
total: "total",
records: "records",
root: "rows",
id: "Id",
repeatitems: false
},
colNames: ['Id', 'FirstName', 'LastName', 'CustomerNumber', 'EmailAddress', 'Subject', 'OrderNumber', 'Comments'],
colModel: [
{ name: 'Id', index: 'Id', width: 90 },
{ name: 'FirstName', index: 'FirstName', width: 100 },
{ name: 'LastName', index: 'LastName', width: 100 },
{ name: 'CustomerNumber', index: 'CustomerNumber', width: 80, align: "right" },
{ name: 'EmailAddress', index: 'EmailAddress', width: 100 },
{ name: 'Subject', index: 'Subject', width: 80 },
{ name: 'OrderNumber', index: 'OrderNumber', width: 80, align: "right" },
{ name: 'Comments', index: 'Comments', width: 250, sortable: false }
],
rowNum: 10,
rowList: [10, 20, 30],
pager: $('#pager1'),
sortname: 'Id',
viewrecords: true,
sortorder: "desc",
caption: "Contact Requests"
});
});
</script>
btw:上面的JSON字符串是脚本服务返回的确切字符串。我可以在我的脚本服务中找到一个断点并获取该字符串,并且我可以在成功时调用ajax脚本中的字符串,并且字符串是相同的。所以我知道这不是脚本服务,我知道json字符串正在传递给jqGrid。
答案 0 :(得分:0)
您的问题是您使用非常旧的模板代码来加载JSON数据。如果您的服务器生成您发布的JSON数据,则可以将代码简化为the following。
在another answers中,您会发现更多代码示例,以防您使用ASMX,它会在您发布时生成其他JSON代码。
答案 1 :(得分:0)
感谢Oleg提供的信息,但事实证明如下:
thegrid.addJSONData(JSON.parse(data.d));
在将数据传递到网格之前,我需要对数据进行JSON.parse。