jqgrid将数据类型设置为jon-获取xml

时间:2011-05-17 12:50:58

标签: jquery jquery-plugins jqgrid

我正在尝试使用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;
        } 

1 个答案:

答案 0 :(得分:1)

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

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

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