JQGrid与web服务不加载数据
问题描述:
我在.net应用程序中使用JQGrid。我使用在线代码示例,它不会抛出任何错误,但它不起作用,要么不知道我做错了什么。有些人在使用JQgrid之前可以查看我的代码,并向我展示iam做错了什么。我能够进入服务器端代码,它不会抛出任何错误。真的不知道我做错了什么。JQGrid与web服务不加载数据
这里是aspx页面:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link rel="stylesheet" type="text/css" media="screen" href="jquery-ui-1.8.17.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="ui.jqgrid.css" />
<script src="jquery.js" type="text/javascript"></script>
<script src="grid.locale-en.js" type="text/javascript"></script>
<script src="jquery.dataTables.min.js" type="text/javascript"></script>
<script src="jquery.jqGrid.min.js" type="text/javascript"></script>
调用gquery负载电网
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#list").jqGrid({
type: "GET",
url: "MyService.asmx/GetRecipie",
contentType: "application/json; charset=utf-8",
dataType: "json",
colNames: ['Inv No', 'Date', 'Amount'],
colModel: [
{ name: 'job_id', index: 'job_id', width: 55 },
{ name: 'job_num', index: 'job_num', width: 90 },
{ name: 'order_num', index: 'order_num', width: 80, align: 'right'}],
pager: jQuery('#pager'),
rowNum: 10,
rowList: [10, 20, 30],
sortname: 'id',
sortorder: "desc",
viewrecords: true,
imgpath: 'themes/basic/images',
caption: 'My first grid',
loadError: Error
});
});
function Error(xhr, st, err) {
jQuery("#rsperror").html("Type: " + st + "; Response: " + xhr.status + " " + xhr.statusText);
}
</script>
</head>
<body>
<table id="list" class="scroll">
</table>
<div id="pager" class="scroll" style="text-align: center;">
</div>
</body>
</html>
我的web
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public string GetRecipie()
{
string strQuery = "SELECT * FROM job where job_id like '%2345%'";
DataTable dtRecipie = null;
Recipie objRecipie = default(Recipie);
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;DATABASE=TESTDB;Data Source=SQL;");
using (con)
{
con.Open();
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strQuery, con))
{
dtRecipie = new DataTable();
sqlAdapter.Fill(dtRecipie);
}
}
List<Recipie> drlist = new List<Recipie>();
foreach (DataRow dr in dtRecipie.Rows)
{
objRecipie = new Recipie();
objRecipie.jobId = Convert.ToInt32(dr["job_id"].ToString());
objRecipie.JobNumber = dr["job_num"].ToString();
objRecipie.OrderNumber = dr["order_num"].ToString();
drlist.Add(objRecipie);
}
JavaScriptSerializer jSearializer = new JavaScriptSerializer();
return jSearializer.Serialize(drlist);
}
}
public class Recipie
{
public int jobId;
public string JobNumber;
public string OrderNumber;
}
}
答
1 - 你的序列化的实体名单无将它们更改为jqgrid能够理解的格式。
您应该返回一个JSON看起来像这样:
{
"page":"1",
"total":4,
"records":"10",
"rows":[
{"id":"1","cell":["Prabir","Shrestha"]},
{"id":"2","cell":["Bill","Gates"]},
{"id":"3","cell":["Steve","Ballmer"]}
]
}
来源:http://blog.prabir.me/post/Using-jqGrid-with-ASPNET-Web-Forms-e28093-Part-I.aspx。本网站介绍了如何将数据转换为此格式。
还有其他的东西,在你使用的webservice中UseHttpGet = false
,而你正在初始化jqgrid type: "GET"
。你需要改变其中之一。
是因为您有UseHttpGet = false,但您的Ajax调用是使用GET而不是POST进行的? – Tuan 2012-02-09 20:16:11
@Tuan:一般来说你是对的,但是HTTP GET不会因为'type:“GET”'而被使用,而是因为参数的正确名称应该是'mtype:“POST”'。参数'dataType:“json”'也是错误的(错误的情况)。所以默认'datatype:“xml”'将被jqGrid使用。发布的代码包含很多错误,因此列表太长。例如,不要从web方法返回'drlist'对象('List'),而是手动调用'JavaScriptSerializer.Serialize'。结果字符串将再次被JSON编码。我可以继续... –
Oleg
2012-02-09 20:48:30
@ user1098028:我建议你阅读[答案](http://stackoverflow.com/a/3161542/315935)。 [这里](http://stackoverflow.com/a/4031603/315935)和[这里]你可以找到一些演示项目的链接,你可以下载。我希望你可以修改演示到你的环境。 – Oleg 2012-02-09 20:52:38