2011-05-17 19 views
1

我正在嘗試使用JSON數據設置jqgrid。
我的問題是,從我的服務返回的數據是以xml格式。
我已經追蹤由電網螢火蟲發出的請求,這裏就是這樣說的:jqgrid將數據類型設置爲jon-獲取xml代替

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 

通知的內容類型字段。
我將它與另一個我正在做的jQuery.ajax請求進行了比較,並且我注意到唯一的區別是在Content-Type字段中。在另一個請求(返回json),內容類型是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

也許您擁有的主要問題:你應該添加額外的參數的jqGrid:

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

然後,來自jqGrid的請求將需要JSON數據。您可以例如下載the old demo project或閱讀更多信息here

+0

@ Oleg-你是最棒的。我一直在尋找類似的問題,似乎你能解決所有這些問題。的確,我的問題是我沒有使用'serializeGridData'函數,並且'_search'參數出現錯誤。再次感謝! – 2011-05-17 13:41:39

+0

@sJhonny:不客氣!我建議你閱讀[答案](http://stackoverflow.com/questions/5500805/asp-net-mvc-2-0-implementation-of-searching-in-jqgrid/5501644#5501644)[另一個演示](http://www.ok-soft-gmbh.com/jqGrid/jqGridDemo.zip)。你不僅可以在ASP.NET MVC中使用大部分的東西,而且可以在任何ASP.NET環境中使用。 – Oleg 2011-05-17 13:48:12

相關問題