javascript 未调用 Jquery Ajax 的成功函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15129097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:36:46  来源:igfitidea点击:

Jquery Ajax's success function not called

c#javascriptjqueryasp.net-mvc

提问by Kokila

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.

控制器方法从数据库中获取记录并存储为客户列表,然后以 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的success函数根本就没有在这里调用。

回答by Dakait

You don't have to specify the content-typebecause 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 dataTypeto jsonwhich is the format in which the client is expecting data from the server. But I really doubt that it could be the cause of any error.

您不必指定content-type,因为它设置在该服务器所期望的数据类型,你是不发送任何所以无需显式设置,其次设置dataTypejson这是格式,其中客户端预期数据从服务器。但我真的怀疑这可能是导致任何错误的原因。

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.

  2. Place a breakpoint inside the Controller to see if the ActionResultis hit when the call is made.

  1. Firebug 或 chrome 工具来检查 ajax 请求并设置返回的状态代码是什么。

  2. 在控制器内部放置一个断点,以查看ActionResult在进行调用时是否命中。

EDIT

编辑

mark your JsonResult with the HttpPostannotation like

用这样的HttpPost注释标记你的 JsonResult

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

回答by Syeda

Replace your line

更换您的线路

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

with this one

有了这个

contentType: "application/json",

and add datatype

并添加数据类型

datatype: 'json'

回答by xlthor

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! So: double check your json format, i.e. with JSON Lint at http://zaach.github.com/jsonlint/or something else. The ajax function is very "tolerant" against any false/erroneous settings, but malformed json code is definitively an error. As long as the error event gets triggered, there IS an error.

今天进入了。问题是,“成功”事件没有被触发,从 jQuery 的角度来看,实际上没有“成功”:服务器返回的 json 代码格式错误!所以:仔细检查你的 json 格式,即http://zaach.github.com/jsonlint/上的 JSON Lint或其他东西。ajax 函数非常“容忍”任何错误/错误的设置,但格式错误的 json 代码绝对是一个错误。只要错误事件被触发,就会出现错误。

回答by AliR?za Ad?yah?i

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. I dont know what properties in your model.

修改 CustomerDAL 模型的警报消息。我不知道你的模型有什么属性。

回答by Maruf

Your code:

您的代码:

return Json(custList);

Change it to:

将其更改为:

return Json(new SelectList(custList));