简体   繁体   English

jQuery Ajax的成功函数未调用

[英]Jquery Ajax's success function not called

I am calling a MVC controller method from Jquery ajax. 我正在从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);
                });
            }
        });
    });

I am having an entity called Customer . 我有一个名为Customer的实体。

Controller Method fetches record from DB and stored as List of Customer and am returning that list in JsonResult type. Controller Method从数据库获取记录并存储为Customer List,并以JsonResult类型返回该列表。

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

But my ajax's success function is not at all called here. 但是我的ajax的成功函数根本没有在这里调用。

You don't have to specify the content-type because it set the type in which the server is expecting the data, you are not sending any so no need to set it explicitly, secondly set the dataType to json which is the format in which the client is expecting data from the server. 您不必指定content-type因为它设置了服务器期望数据的类型,您没有发送任何内容,因此无需显式设置它,其次将dataType设置为json ,即客户端期望来自服务器的数据。 But I really doubt that it could be the cause of any error. 但是我真的怀疑这可能是导致任何错误的原因。

Add an error callback to make sure something went wrong on the way back like 添加错误回调以确保返回途中出现问题,例如

try 尝试

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

Use 使用

  1. Firebug or chrome tools to examine the ajax request and set what is the status code returned. Firebug或chrome工具检查ajax请求并设置返回的状态码是什么。

  2. Place a breakpoint inside the Controller to see if the ActionResult is hit when the call is made. 在控制器内放置一个断点,以查看调用时是否点击了ActionResult

EDIT 编辑

mark your JsonResult with the HttpPost annotation like HttpPost注释将您的JsonResult标记HttpPost

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

Replace your line 更换线

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

with this one 与这个

contentType: "application/json",

and add datatype 并添加数据类型

datatype: 'json'

Got into that today. 今天就进入了。 The problem, that the "success" event wasn't fired was, that there actually wasn't a "success" from the jQuery perspective: the json code returned by the server was malformed! 问题是,未触发“成功”事件的原因是,从jQuery的角度来看实际上并没有“成功”:服务器返回的json代码格式错误! So: double check your json format, ie with JSON Lint at http://zaach.github.com/jsonlint/ or something else. 因此:仔细检查您的json格式,即使用JSON Lint(位于http://zaach.github.com/jsonlint/)或其他方法。 The ajax function is very "tolerant" against any false/erroneous settings, but malformed json code is definitively an error. 对于任何错误/错误设置,ajax函数都非常“容忍”,但是格式错误的json代码肯定是一个错误。 As long as the error event gets triggered, there IS an error. 只要错误事件被触发,就存在错误。

You have an object list as List in ajax-success function. 在ajax-success函数中,您有一个对象列表作为List。

to parse it try this 解析它试试这个

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

controller 控制者

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

    return Json(custList);
}

for example assume that your CustomerDAL model 例如,假设您的CustomerDAL模型

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

Modify alert message for your CustomerDAL model. 修改您的CustomerDAL模型的警报消息。 I dont know what properties in your model. 我不知道您的模型中有哪些属性。

Your code: 您的代码:

return Json(custList);

Change it to: 更改为:

return Json(new SelectList(custList));

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

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