通过JSON调用jqGrid为空 [英] jqGrid is empty with JSON call
问题描述
我的jqGrid随JSON调用而变空了.虽然它与datatype ='jsonstring'一起使用,但不适用于json类型.这是我的JSON
My jqGrid is coming empty with JSON call. Though it is working with datatype='jsonstring' but not with json type. here is my JSON
{
"d": {
"total": 6,
"page": 1,
"records": 6,
"rows": [
{
"id": 1,
"Name": "James",
"EMPID": "0000000056",
"EMPDATE": "",
"JOBTYPE": "REQ",
"DEPTID": "FIN",
"STATUS": "P1"
},
{
"id": 2,
"Name": "James",
"EMPID": "R2",
"EMPDATE": "",
"JOBTYPE": "REQ",
"DEPTID": "FIN",
"STATUS": "P1"
},
{
"id": 3,
"Name": "James",
"EMPID": "V2",
"EMPDATE": "",
"JOBTYPE": "VOU",
"DEPTID": "FIN",
"STATUS": ""
},
{
"id": 4,
"Name": "James",
"EMPID": "V1",
"EMPDATE": "",
"JOBTYPE": "VOU",
"DEPTID": "FIN",
"STATUS": ""
},
{
"id": 5,
"Name": "James",
"EMPID": "009017",
"EMPDATE": "",
"JOBTYPE": "PY",
"DEPTID": "",
"STATUS": "V2"
},
{
"id": 6,
"Name": "James",
"EMPID": "009018",
"EMPDATE": "",
"JOBTYPE": "PY",
"DEPTID": "",
"STATUS": "V1"
}
]
}
}
并且我从javascript进行的jqGrid调用是
and my jqGrid call from javascript is
function getgrid()
{
$("#list").jqGrid({
url:'http://10.240.26.41/GetGridFields',
datatype: 'json',
mtype: 'GET',
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
//serializeGridData: function (postData) {
// return JSON.stringify(postData);
// },
jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
colModel: [
{ name: 'id', key: true, width: 60, align: "center", hidden: false },
{ name: 'Name', width: 80, sortable: false, hidden: false },
{ name: 'EMPID', width: 180, sortable: false, hidden: false },
{ name: 'EMPDATE', width: 180, sortable: false, hidden: false },
{ name: 'JOBTYPE', width: 180, sortable: false, hidden: false },
{ name: 'DEPTID', width: 180, sortable: false, hidden: false },
{ name: 'STATUS', width: 180, sortable: false, hidden: false }
],
rowNum: 10,
rowList: [10, 20, 300],
pager: "#pager",
viewrecords: true,
gridview: true,
rownumbers: true,
height: 230,
caption: 'Emp Detail'
})
}
请让我知道我在哪里错了.可以与jsonstring一起正常工作.
Please let me know where i am wrong here. It is working fine with jsonstring.
推荐答案
如何在演示,您的jqGrid可以读取JSON数据.我在演示中仅做了最小限度的优化更改,这些更改对您的主要问题并不重要.
How you can see on the demo your jqGrid can read your JSON data. I made in the demo only minimal optimization changes which don't important for your main problem.
所以我想,您可能遇到了使用完整URL的问题(应该使用'/GetGridFields'
而不是'http://10.240.26.41/GetGridFields'
),因为由于同一出处政策.另一个可能的原因是您输入了错误的'Content-Type'
或其他HTTP标头.您可以根据提琴手,
So I suppose, that you have either the problem with the usage of full URL (you should use '/GetGridFields'
instead of 'http://10.240.26.41/GetGridFields'
) because you can't get Ajax call to another IP host because of the same origin policy. One other possible reason is that you have wrong 'Content-Type'
or some other HTTP header. You can examine the HTTP headers with respect of Fiddler, Firebug or just with Developer Tools of IE or Chrome (see "Network" tab).
无论如何,我建议您将loadError
处理程序包括到jqGrid中.有关详细信息,请参见答案.
In any way I would recommend you to include loadError
handler to jqGrid. See the answer for details.
这篇关于通过JSON调用jqGrid为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!