gpt4 book ai didi

c# - 未调用 Jquery Ajax 成功函数

转载 作者:太空狗 更新时间:2023-10-30 00:53:47 26 4
gpt4 key购买 nike

我正在从 Jquery ajax 调用 MVC Controller 方法。

$(document).ready(function () {
$.ajax({
type: "POST",
url: "/Customer/GetDetails",
contentType: "application/json; charset=utf-8",
async: false,
cache: false,
success: function (data) {
$.each(data, function (index, value) {
alert(value);
});
}
});
});

我有一个名为客户的实体。

Controller 方法从数据库中获取记录并存储为客户列表,并以 JsonResult 类型返回该列表。

public JsonResult GetDetails()
{
CustomerDAL customer = new CustomerDAL();
IList<Customer> custList = customer.GetDetail();
return Json(custList);
}

但是我的ajax的success函数并没有在这里调用。

最佳答案

您不必指定 content-type 因为它设置了服务器期望数据的类型,您没有发送任何数据所以不需要显式设置,其次设置dataTypejson 这是客户端期望来自服务器的数据的格式。但我真的怀疑这可能是任何错误的原因。

添加一个错误回调以确保在返回途中出现问题

尝试

$.ajax({
type: "POST",
url: "/Customer/GetDetails",
dataType:'json',
async: false,//WHY??
cache: false,
success: function (data) {
alert("success");
},
error:function(){
alert("something went wrong");
}
});

使用

  1. Firebug 或 chrome 工具,用于检查 ajax 请求并设置返回的状态代码。

  2. 在 Controller 中放置一个断点,以查看调用时是否命中 ActionResult

编辑

HttpPost注解标记你的JsonResult

[HttpPost]
public JsonResult GetDetails()
{
CustomerDAL customer = new CustomerDAL();
IList<Customer> custList = customer.GetDetail();
return Json(custList);
}

关于c# - 未调用 Jquery Ajax 成功函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com