我看过其他类似的线程,我似乎无法让我的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"]]}
答案 0 :(得分:0)
经过一番搜索后,我设法解决了自己的问题。如果有人在将来遇到同样的问题,下面是更正后的代码。
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是空的!在此开始了另一个post,希望有人得到了解决方案。
谢谢!