Asp.net 使用Ajax调用C#集合到GridView
为什么调用此集合时出错?将此集合绑定到Gridview控件所需的全部 C#代码:Asp.net 使用Ajax调用C#集合到GridView,asp.net,entity-framework,jquery,Asp.net,Entity Framework,Jquery,为什么调用此集合时出错?将此集合绑定到Gridview控件所需的全部 C#代码: [WebMethod] 公共静态列表GetAllCustomers() { 使用(主实体上下文=新的主实体() { 返回context.Customer.ToList(); } 受保护的无效页面加载(对象发送方、事件参数e) { DataTable=新的DataTable(); 表.列.添加(“id”); 表.列.添加(“名称”); 表.列.添加(“年龄”); table.Rows.Add(); GridView1
[WebMethod]
公共静态列表GetAllCustomers()
{
使用(主实体上下文=新的主实体()
{
返回context.Customer.ToList();
}
受保护的无效页面加载(对象发送方、事件参数e)
{
DataTable=新的DataTable();
表.列.添加(“id”);
表.列.添加(“名称”);
表.列.添加(“年龄”);
table.Rows.Add();
GridView1.DataSource=表格;
GridView1.DataBind();
}
Ajax代码:
function GetCustomers() {
$.ajax({
type: "POST",
url: "12.aspx/GetAllCustomers",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: function (data) {
alert(data.d);
}
});
function OnSuccess(data) {
for (var i = 0; i < data.d.length; i++) {
$("#GridView1").append("<tr><td>" + data.d[i].Id + "</td><td>" + data.d[i].Name + "</td><td>" + data.d[i].Age + "</td></tr>");
}
}
函数GetCustomers(){
$.ajax({
类型:“POST”,
url:“12.aspx/GetAllCustomers”,
数据:“{}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:一旦成功,
错误:函数(数据){
警报(数据d);
}
});
函数OnSuccess(数据){
对于(变量i=0;i问题在于
$。ajax
错误处理程序:
error: function (data) {
alert(data.d);
}
错误处理程序的编写方法如下:错误处理程序的编写方法如下:
error: function (jqXHR, textStatus, errorThrown ) {
console.log(jqXHR);
console.log(jqXHR.responseText);
}
使用这种方式编写的错误处理程序,您可以查看Chrome Developer Tools的控制台窗口,并查看jqXHR.responseText的值,其中包含错误的详细错误描述
不过,在部署web应用程序时,请小心使用更有意义的错误消息替换console.log语句
以下是jQuery API文档的链接(查找错误处理程序描述):
希望这有帮助
问候,,
Uros那么jqXHR.responseText的价值是什么?
error: function (jqXHR, textStatus, errorThrown ) {
console.log(jqXHR);
console.log(jqXHR.responseText);
}