![](/img/trans.png)
[英]Displaying data into a graph in web service ASP.NET MVC Framework
[英]jqGrid not displaying data (asp.net web service with json)
我瀏覽了其他類似的線程,但似乎無法讓jqGrid顯示我要綁定的數據。 colModal和colNames確實很不錯,但是沒有數據。 我在這里做錯了什么? 提前致謝!
網絡服務:
Structure JSONObject2
Public page As String
Public total As String
Public records As String
Public rows As List(Of List(Of String))
End Structure
<WebMethod(EnableSession:=True)> _
<System.Web.Script.Services.ScriptMethod(ResponseFormat:=System.Web.Script.Services.ResponseFormat.Json)> _
Public Function GetData() As Object
Dim jsObj As JSONObject2
jsObj.rows = New List(Of List(Of String))
jsObj.page = 1
jsObj.records = 3
jsObj.total = 3
If Not IsNothing(Session.Item("DataTable")) Then
Dim dt As DataTable = Session.Item("DataTable")
For Each dr As DataRow In dt.Rows
Dim newrow As New List(Of String)
For Each dc As DataColumn In dt.Columns
newrow.Add(dr(dc))
Next
jsObj.rows.Add(newrow)
Next
End If
Return jsObj
End Function
jqgrid聲明:
jQuery("#dataGrid").jqGrid({
jsonReader : {
root:"rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
id: "0"
},
url: 'WebService.asmx/GetData',
datatype: "json",
mtype: "POST",
ajaxGridOptions: {
contentType: "application/json; charset=utf-8",
},
colNames: [<%= colName %>],
colModel: [<%= colModal %>],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#dataGrid_Pager',
sortname: 'name',
viewrecords: true,
sortorder: "name",
caption: "JSON Example"
});
我使用普通的jquery ajax請求數據。
$.ajax({
type: "POST",
url: "WebService.asmx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(JSON.stringify(result));
}
});
數據返回:
{"page":"1","total":"3","records":"3","rows":[["rw_administrator","Reports Administrator","account"],["rw_operator","Reports Operator","account"],["rw_monitor","Reports Monitor","account"]]}
經過一些搜索,我設法解決了自己的問題。 如果將來有人遇到相同的問題,下面是更正的代碼。
jQuery("#dataGrid").jqGrid({
jsonReader : {
root:"rows",
page: "page",
total: "total",
records: "records",
id: "0"
},
url: 'WebService.asmx/GetData',
datatype: "json",
mtype: "POST",
ajaxGridOptions: {
contentType: "application/json; charset=utf-8"
},
serializeGridData: function (data) {
return JSON.stringify(data);
},
colNames: [<%= colName %>],
colModel: [<%= colModal %>],
rowNum: 10,
rowList: [10, 20, 30],
pager: '#dataGrid_Pager',
sortname: 'name',
viewrecords: true,
sortorder: "name",
caption: "JSON Example"
});
但是,使用以下方法出現了另一個問題:
ajaxGridOptions: {
contentType: "application/json; charset=utf-8"
},
我的postData為空! 對此發表了另一篇文章 ,希望有人能找到解決方案。
謝謝!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.