在jqGrid中顯示我的json數據時遇到了麻煩。在jqGrid中加載JSON數據
我在這個論壇上搜索了很多,並嘗試過各種形式使它工作。我很抱歉,如果這已經得到解答,但我真的需要幫助。
在服務器頁面我只是用JavaScriptSerializer發送數據和jsonreader函數的默認參數(這個工作確定)。
我現在需要進行分頁,並改變了我的服務器網頁代碼與SIDX,SORD,頁,行參數工作。
從服務器生成的字符串如下所示:
{"total":"344","page":"1","records":"8577","root":[{"Id":"1","SerialNumber":"132","Name":"ServerName"},...]}
這裏是我的jQuery代碼:
$("#list").jqGrid({
datatype: "json",
mtype: 'GET',
url:'https://server/handlerpage.ashx',
colNames:['Id','SerialNumber','Name'],
colModel :[
{name:'Id', index:'Id', jsonmap:"Id", width:20},
{name:'Name', index:'Name', jsonmap:"Name", width:120},
{name:'SerialNumber', index:'SerialNumber', jsonmap:"SerialNumber", width:100}],
jsonreader: {repeatitems:false,id:'Id',root:'root'},
pager: '#pager',
rowNum:25,
rowList:[25,50,75,100],
sortname: 'Id',
viewrecords:true,
gridview: true,
height:"400",
width:"700",
caption: 'Select from existing server',
loadtext:'Loading, please wait'
}).navGrid("#pager", { edit: false, add: false, search: true, del: false });
您的代碼中的主要錯誤是使用'jsonreader'而不是'jsonReader'。你可以刪除所有'jsonmap'屬性和'index'。我建議你另外不要使用像''https:// server/handlerpage.ashx'這樣的'url'。對應於Ajax [相同來源策略](http://en.wikipedia.org/wiki/Same_origin_policy),您必須使用與當前頁面相同的「https:// server /」前綴。所以你最好使用'url:'handlerpage.ashx''或'url:'/ handlerpage.ashx''。 – Oleg 2012-03-27 19:48:03
謝謝Oleg,我測試了jsonReader(大寫R),它確實有效。也改變了網址以遵守政策並且也工作。 – fabianmunoz 2012-03-27 21:02:56
不客氣!有時候錯誤非常小(只有一個字母),但正因爲如此,可能很難找到它。 – Oleg 2012-03-27 21:09:35