0

我正在使用 asmx 服务返回数据以在 jqGrid 中显示。我可以看到在完整回调中返回了 json 数据。这就是完整回调中的 json 数据的样子{"d":[{"__type":"HHSC.CTF.Business.BatchReceiptModel","BReceiptId"....。我不确定为什么它前面d:还有数据的类型名称。这是我的 jqGrid 设置看起来像

$("#list").jqGrid({
    url: "../../WebServices/BatchReceiptsWebService.asmx/BatchReceiptsTable",
    datatype: "json",
    mtype: 'POST',   
    ajaxGridOptions: { contentType: 'application/json; charset=utf-8',
        success: function (data, status) {

        },
        complete: function (xhr) {

        },
        error: function (jqXHR, textStatus, errorThrown) {

        }
    },
    serializeGridData: function (postData) {
        return JSON.stringify(postData);
    },
    jsonReader: {
        repeatitems: false,
        id: "BReceiptId",
        page: function (obj) { return 1; },
        total: function (obj) { return 1; },
        root: function (obj) { return obj; },
        records: function (obj) {
            return obj.d.length; 
        }
    },
    colNames: ['BReceiptId', 'ReceiptNumber', 'ReceiptAmount'],
    colModel: [
                    { name: 'BReceiptId', index: 'BReceiptIdnId', width: 100 },
                    { name: 'ReceiptNumber', index: 'ReceiptNumber', width: 150 },
                    { name: 'ReceiptAmount', index: 'ReceiptAmount', align: 'right', width: 100 }
                ],
    rowNum: 10,
    loadonce: true,
    gridview: true,
    rownumbers: true,
    rowList: [10, 20, 30],
    viewrecords: true

});
4

2 回答 2

2

您不能通过使用来覆盖successerror回调. 如果您检查jqGrid的源代码,您将看到 jqGrid 使用回调。内部回调 jqGrid 处理服务器响应并填充网格,然后隐藏“正在加载...” div。通过在你的内部定义回调jqGrid 使用的 Ajax 处理。jQuery.ajaxajaxGridOptionssuccesssuccesserrorajaxGridOptions

于 2013-08-21T07:34:48.180 回答
1

你的 jsonReader 看起来有点笨拙。从 ASP.NET 3.5 开始,'d' 字符包装了 ASMX 服务调用的 JSON 返回。见这里:http ://encosia.com/a-break-change-between-versions-of-aspnet-ajax/

尝试:

        jsonReader: {
            repeatitems: false,
            root: "d.rows", // or d dot whatever property name you use for the collection of data
            page: "d.page", // or d dot whatever property name you use for the current page 
            total: "d.total", // or d dot whatever property name you use for total pages
            records: "d.records", // or d dot whatever property name you use for total record count
            id: "BReceiptId",
        },

见这里:http ://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data

我返回一个看起来像这样的对象:

public class GridData<T>
{
    public int Total { get; set; }
    public int Page { get; set; }
    public int Records { get; set; }
    public List<T> Rows { get; set; }
    public object UserData { get; set; }
}

所以我的 jsonReader 如下(注意区分大小写):

        jsonReader: {
            repeatitems: false,
            root: "d.Rows",
            page: "d.Page",
            total: "d.Total",
            records: "d.Records",
            userdata: "d.UserData",
            id: "Id"
        },
于 2013-08-20T20:31:09.340 回答