繁体   English   中英

jQuery Ajax的成功函数未调用

[英]Jquery Ajax's success function not called

我正在从Jquery ajax调用MVC控制器方法。

$(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);
                });
            }
        });
    });

我有一个名为Customer的实体。

Controller Method从数据库获取记录并存储为Customer List,并以JsonResult类型返回该列表。

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

但是我的ajax的成功函数根本没有在这里调用。

您不必指定content-type因为它设置了服务器期望数据的类型,您没有发送任何内容,因此无需显式设置它,其次将dataType设置为json ,即客户端期望来自服务器的数据。 但是我真的怀疑这可能是导致任何错误的原因。

添加错误回调以确保返回途中出现问题,例如

尝试

$.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. 在控制器内放置一个断点,以查看调用时是否点击了ActionResult

编辑

HttpPost注释将您的JsonResult标记HttpPost

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

更换线

 contentType: "application/json; charset=utf-8",

与这个

contentType: "application/json",

并添加数据类型

datatype: 'json'

今天就进入了。 问题是,未触发“成功”事件的原因是,从jQuery的角度来看实际上并没有“成功”:服务器返回的json代码格式错误! 因此:仔细检查您的json格式,即使用JSON Lint(位于http://zaach.github.com/jsonlint/)或其他方法。 对于任何错误/错误设置,ajax函数都非常“容忍”,但是格式错误的json代码肯定是一个错误。 只要错误事件被触发,就存在错误。

在ajax-success函数中,您有一个对象列表作为List。

解析它试试这个

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/Customer/GetDetails", // or better way '@Url.Action("GetDetails", "Customer")'
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            $.each(data, function (index, customer) {
                alert(customer.id, customer.name, customer.surname);
            });
        }
    });
});

控制者

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

    return Json(custList);
}

例如,假设您的CustomerDAL模型

class CustomerDAL
{ 
    public int id { get; set; }
    public string name { get; set; }
    public string surname { get; set; }
}

修改您的CustomerDAL模型的警报消息。 我不知道您的模型中有哪些属性。

您的代码:

return Json(custList);

更改为:

return Json(new SelectList(custList));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM