jqGrid 不加载数据 [英] jqGrid Not Loading Data
问题描述
我确定这是我缺少的一些简单的东西,但我似乎无法找到它.我在这里指定了一个简单的 jqGrid:
I'm sure this is something simple that I'm missing, but I just can't seem to find it. I have a simple jqGrid specified here:
$('#mainGrid').jqGrid({
datatype: 'local',
colNames: ['id', 'name'],
colModel: [
{ name: 'id', index: 'id', width: 100 },
{ name: 'name', index: 'name', width: 300 }
],
rowNum: 9999,
sortname: 'name',
viewrecords: true,
sortorder: 'asc',
data: [{"id":"924c18a4-cad6-4b6a-97ef-f9ca61614530","name":"Pathway 1"},{"id":"54897f40-49ab-4abd-acac-6047006c7cc7","name":"Pathway 2"},{"id":"61542c48-102f-4d8e-ba9f-c24c64a20d28","name":"Pathway 3"},{"id":"c4ca9575-7353-4c18-b38a-33b383fcd8b2","name":"Pathway 4"}]
});
这会正确加载.简单的概念证明.现在我尝试用对服务器资源的调用来替换本地数据:
This loads correctly. Simple proof of concept. Now I try to replace the local data with a call to a server resource:
$('#mainGrid').jqGrid({
url: 'AJAXHandler.aspx',
datatype: 'json',
colNames: ['id', 'name'],
colModel: [
{ name: 'id', index: 'id', width: 100 },
{ name: 'name', index: 'name', width: 300 }
],
rowNum: 9999,
sortname: 'name',
viewrecords: true,
sortorder: 'asc'
});
服务器资源返回相同的数据.但是网格没有加载数据.(至少,它没有显示任何记录.)我已经通过 FireBug 确认该资源确实被调用并且正在返回预期的数据.
The server resource returns the same data. But the grid isn't loading the data. (At least, it's not showing any records.) I've confirmed with FireBug that the resource is indeed being called and is returning the expected data.
一开始我以为资源响应中的内容类型应该改成application/json
,但是这并没有什么区别.阻止网格加载数据的响应是否还有其他问题?
At first I thought that the content type in the resource's response should be changed to application/json
, but that made no difference. Is there something else wrong with that response that's preventing the grid from loading the data?
服务器资源的 Firebug 输出:
Firebug output for the server resource:
Response Headers
Cache-Control private
Content-Length 261
Content-Type text/html; charset=utf-8
Server Microsoft-IIS/6.0
MicrosoftSharePointTeamSe... 12.0.0.6219
X-Powered-By ASP.NET
X-AspNet-Version 2.0.50727
Set-Cookie WSS_KeepSessionAuthenticated=80; path=/
Date Sat, 23 Apr 2011 14:35:43 GMT
Request Headers
Host cyber0ne.com
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16
Accept application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://cyber0ne.com/dovetail_pages/Member/Pathways.aspx?MemberID=b428e0a7-dd55-de11-8e97-0016cfe25fa3
Cookie WSS_KeepSessionAuthenticated=80
Params
_search false
nd 1303569347783
page 1
rows 9999
sidx name
sord asc
Response
[{"id":"4d4b8249-b5f9-4da6-aba2-bf3af588d560","name":"Pathway 1"},{"id":"230184e6-44cc-4274-97fd-b455440cd9c0","name":"Pathway 2"},{"id":"7f938218-b963-495f-9646-f3cfb1e63ea1","name":"Pathway 3"},{"id":"2b17f23e-5500-4b01-ac1c-df2de90dc511","name":"Pathway 4"}]
更新:
根据@Paul Creasey 下面的回答,现在的回复内容是:
Per @Paul Creasey's answer below, the response content is now:
{"total":4,"page":1,"records":4,"rows":[{"id":"55132687-b0bd-4c89-97cb-122d127429eb","name":"Pathway 1"},{"id":"123ba476-1560-4148-ae96-968bdd10e190","name":"Pathway 2"},{"id":"43f5660b-141c-4ccc-848e-6b41667b399a","name":"Pathway 3"},{"id":"b0d21316-d07d-4b46-8011-89c3cb07a8f6","name":"Pathway 4"}]}
行为略有改变.网格现在显示正在加载",但仍未加载数据.
The behavior has changed slightly. The grid now says "Loading" but still doesn't load the data.
推荐答案
根据文档 这里,预期的json格式为:
According to the docs here, the expected json format is:
{
"total": "xxx",
"page": "yyy",
"records": "zzz",
"rows" : [
{"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
{"id" :"2", "cell":["cell21", "cell22", "cell23"]},
...
]
}
因此你的网络服务应该返回这个:
Therefore you web service should be returning this:
{
"total": "4",
"page": "1",
"records": "4",
"rows" : [
{
"id": "4d4b8249-b5f9-4da6-aba2-bf3af588d560",
"name": "Pathway 1"
},
{
"id": "230184e6-44cc-4274-97fd-b455440cd9c0",
"name": "Pathway 2"
},
{
"id": "7f938218-b963-495f-9646-f3cfb1e63ea1",
"name": "Pathway 3"
},
{
"id": "2b17f23e-5500-4b01-ac1c-df2de90dc511",
"name": "Pathway 4"
}
]
}
目前你只有 rows 数组.
At the moment you only have the rows array.
您可以实现自己的函数来读取 json,但我从未这样做过,请参阅上面链接的jsonReader as Function"部分.
You could implement your own function to read the json, but i've never done this, see the "jsonReader as Function" section of the link above.
一开始我错了,要么你应该将repeatitems标志设置为false:
I was wrong initially, either you should set the repeatitems flag to false:
jsonReader : {
repeatitems: false
},
并使用上面的 json(我认为!)或返回这样的数据:
and use the json above (i think!) or return data like this:
{
"total": "4",
"page": "1",
"records": "4",
"rows" : [
{
"id": "4d4b8249-b5f9-4da6-aba2-bf3af588d560",
"cells" : ["Pathway 1"]
},
{
"id": "230184e6-44cc-4274-97fd-b455440cd9c0",
"cells" : ["Pathway 2"]
},
{
"id": "7f938218-b963-495f-9646-f3cfb1e63ea1",
"cells" : ["Pathway 3"]
},
{
"id": "2b17f23e-5500-4b01-ac1c-df2de90dc511",
"cells" : ["Pathway 4"]
}
]
}
我一直做后者,但我认为前者可能更好!
I've always done the latter, but i think the former is probably better!
这篇关于jqGrid 不加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!