我最终放弃了 JSON(使用 ASP.NET)而只使用了 XML。然后一切正常。确保 asmx 的返回类型是 XmlDocument
可以在JQGrid wiki上找到为使其工作(至少在客户端)所需做的所有事情的一个很好的总结
查看他们的示例 XML 并确保您的服务返回遵循相同架构的缓冲区(最好使用Fiddler进行验证)。
更新 - 一些代码示例
这是创建网格的客户端脚本
var mygrid = $("#list").jqGrid({
url: '../../webServices/callsGridService.asmx/getCallsGridDataXML?nd=' + new Date().getTime(),
datatype: 'xml',
mtype: 'POST',
contentType: "text/xml",
colNames: ['Call ID', 'Date / Time', 'Duration'],
colModel: [
{ name: 'callId', index: 'callId', align: "right", key: true },
{ name: 'callTime', index: 'callTime', sorttype: 'date' },
{ name: 'duration', index: 'duration', align: "right" }
],
pager: $('#pager'),
rowNum: 10,
rowList: [10, 25, 50, 100],
sortname: 'callTime',
viewrecords: true,
sortorder: "desc",
height: "100%",
multiselect: true,
rownumbers: true,
gridview: true,
autowidth: true,
caption: "Calls"
})
这是服务代码(VB.NET):
Public Function getCallsGridDataXML() As XmlDocument
Dim xmlRet As New XmlDocument
Dim ret As New StringBuilder
m_pageNum = CInt(HttpContext.Current.Request.Form.Item("page"))
If m_pageNum = Nothing OrElse m_pageNum = 0 Then
m_pageNum = 1
End If
m_pageSize = CInt(HttpContext.Current.Request.Form.Item("rows"))
If m_pageSize = Nothing OrElse m_pageSize = 0 Then
m_pageSize = 10
End If
m_sortItem = CStr(HttpContext.Current.Request.Form.Item("sidx"))
m_sortOrder = CStr(HttpContext.Current.Request.Form.Item("sord"))
Dim dt As DataTable
dt = Session(SESSION_CALLS_GRID_DATA)
Dim myView As DataView = dt.DefaultView
If m_sortItem IsNot Nothing AndAlso m_sortOrder IsNot Nothing Then
myView.Sort = m_sortItem & " " & m_sortOrder
End If
ret.Append("<?xml version='1.0' encoding='utf-8'?>")
ret.Append("<rows>")
ret.Append("<page>" & m_pageNum & "</page>")
ret.Append("<total>" & Math.Floor(dt.Rows.Count / m_pageSize) & "</total>")
ret.Append("<records>" & dt.Rows.Count & "</records>")
For i As Integer = (m_pageNum - 1) * m_pageSize To Math.Min(dt.Rows.Count - 1, m_pageNum * m_pageSize - 1)
ret.Append("<row>")
Dim cellCount As Integer = 0
ret.Append("<cell>" & Server.HtmlEncode(myView(i)("callId")) & "</cell>")
ret.Append("<cell>" & Server.HtmlEncode(myView(i)("callTime")) & "</cell>")
ret.Append("<cell>" & Server.HtmlEncode(myView(i)("duration")) & "</cell>")
ret.Append("</row>")
Next
ret.Append("</rows>")
xmlRet.LoadXml(ret.ToString)
Return xmlRet
End Function
您可以看到我正在将 XML 构建为字符串,然后将其加载到 XMLDocumennt 中。我不能说我知道这是最好的方法。您可以直接在文档上构建 XML DOM。