【问题标题】:jqgrid set datatype to jon- gets xml insteadjqgrid 将数据类型设置为 jon-gets xml
【发布时间】:2011-05-17 12:50:58
【问题描述】:

我正在尝试使用 JSON 数据设置 jqgrid。
我的问题是我的服务返回的数据是 xml 格式。
我在 firebug 中跟踪了网格发送的请求,它是这样说的:

Request Headers
Host    localhost
User-Agent  Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer http://localhost/sample/sampleUserSearchPage.htm
Content-Length  60
Cookie  ASP.NET_SessionId=yfx42t45b0nidn45yztqzsun

注意 Content-Type 字段。
我将此与我正在发出的另一个 jQuery.ajax 请求进行了比较,我注意到唯一的区别在于 Content-Type 字段。在另一个请求(返回 json)中,Content-Type 是 application/json; charset=UTF-8;
我认为这是问题所在,但我在 jqgrid 文档上找不到如何更改它。
附件是我的jQuery代码:

$("#grid").jqGrid({
        url: 'SampleScriptService.asmx/GetGridData',
        datatype: "json",
        mtype: "POST",
        jsonReader : { root: "rows" },
        colNames: ['Username', 'Full Name', 'Monitor?', 'Schedule?', 'Reports?', 'Administrator?', 'Password'],
        colModel: [
            { name: 'username', key: true, index: 'id', jsonmap: 'Username' },
            { name: 'fullname', index: 'invdate', width: 90 , jsonmap: 'FullName' },
            { name: 'ismonitor', index: 'name', width: 100, jsonmap: 'IsMonitor' },
            { name: 'isschedule', index: 'amount', width: 80, jsonmap: 'IsSchedule' },
            { name: 'isreports', index: 'tax', width: 80, jsonmap: 'IsReports' },
            { name: 'isadministrator', index: 'total', width: 80, jsonmap: 'IsAdministrator' },
            { name: 'password', index: 'note', width: 150, jsonmap: 'Password' }
             ],
        rowNum: 10,
        viewrecords: true,
        caption: "Simple data manipulation",
    });

和web服务方法:

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public GridData GetGridData(int page, int rows, /*string sixd,*/ string sord)
        {
            var arr= new UsersController().SearchUsers("", 10, 0).ToArray(); //this returns an array of User objects.
            GridData retVal = new GridData() { page = 1, records = 6, total = 34, rows = arr };

            return retVal;
        } 

【问题讨论】:

    标签: jquery jquery-plugins jqgrid


    【解决方案1】:

    可能是您遇到的主要问题:您应该向 jqGrid 添加其他参数:

    ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
    serializeGridData: function (postData) {return JSON.stringify(postData);}
    

    那么来自 jqGrid 的请求将需要 JSON 数据。例如,您可以下载the old demo project 或阅读更多信息here

    【讨论】:

    • @Oleg- 你是最棒的。我一直在寻找类似的问题,似乎你能够解决所有这些问题。确实我的问题是我没有使用“serializeGridData”函数,并且在“_search”参数上出现错误。再次感谢!
    • @sJhonny:不客气!我建议您阅读 the answeranother demo。您不仅可以在 ASP.NET MVC 中使用大部分内容,还可以在任何 ASP.NET 环境中使用。
    猜你喜欢
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2017-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多