2010-06-30 39 views
0

jqGrid一直在踢我的屁股(以及本網站上的其他人)。我似乎無法從web服務獲取JSON數據以在使用addJSONData方法時加載到jqGrid中。你可以在ASP.net中使用web服務和JavaScript使用jqGrid嗎?

有誰知道這是可能做到的嗎?我沒有在ASP.NET 3.5中使用MVC只是一個簡單的WebProject web服務。

我使用的是最新版本的jqGrid 3.5。

我不知道該怎麼做。目前我正在試圖加載僅1行,我在我的WS這樣返回一個字符串:

"Page:1,Total:1,Records:1,Rows:[name: Jeff V title: Programmer]" 

然後傳遞到我的javascript爲: {「d」:「頁:1,總計: 1,記錄:1,行:名稱:傑夫·瓦卡羅標題:程序員]「}

我的jQuery代碼如下:

jQuery(document).ready(function() { 
    jQuery("#list").jqGrid({ 
     datatype: processrequest, 
     mtype: 'POST', 
     colNames: ['Name', 'Title'], 
     colModel: [ 
     { name: 'name', index: 'name', width: 55 }, 
     { name: 'title', index: 'title', width: 90 } 
     ], 
     pager: jQuery('#pager'), 
     rowNum: 10, 
     rowList: [10, 20, 30], 
     sortname: 'id', 
     sortorder: "desc", 
     viewrecords: true, 
     imgpath: 'themes/basic/images', 
     caption: 'My first grid' 
    }); 
}); 


function processrequest(postdata) { 
    $(".loading").show(); 
    $.ajax({ 
     type: "POST", 
     data: "{}", 
     datatype: "clientside", 
     url: "../webServices/myTestWS.asmx/testMethod", 
     contentType: "application/json; charset-utf-8", 
     complete: function (jsondata, stat) { 
      if (stat == "success") { 
       jQuery("#list")[0].addJSONData(eval("(" + jsondata.responseText + ")")); 
       $(".loading").hide(); 
      } else { 
       $(".loading").hide(); 
       alert("Error with AJAX callback"); 
      } 
     } 
    }); 
} 

我已經試過了各種的addJSONData代碼的不同變化。有誰知道這是可能做到的嗎?

任何幫助表示讚賞!

感謝

回答

3

首先你所有的Web服務的應該返回一個類的實例與性能pagetotalrecordsrows等。如果web方法的屬性爲[ScriptMethod (ResponseFormat = ResponseFormat.Json)],則類實例將被正確轉換爲JSON數據。

您可以使用ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }參數和jsonReader(請參閱http://www.trirand.com/jqgridwiki/doku.php?id=wiki:retrieving_data#json_data)在jqGrid中加載數據。 Jqgrid 3.7 does not show rows in internet explorerBest way to change jqGrid rowNum from ALL to -1 before pass to a web service的信息可能對您有所幫助。

最後imgpath在jqGrid的3.5版本中不再受支持。

+0

OK ......在更改我的WS後,回拉一個非常簡單的測試對象,它返回總數,頁面,記錄和行(列表數據類型),然後在WS中使用以下內容: _ _ 我還添加: jsonReader:{ 根: 「行」, 頁: 「頁」, 總: 「總」, 記載: 「記錄」, repeatitems :false, id:「ID」//帶有PK的列的索引 }, 給我的J avaScript文件。 畢竟它終於奏效了。 – webdad3 2010-06-30 20:54:56

相關問題