jquery ajax完成功能未触发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16744066/
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-08-26 17:41:38  来源:igfitidea点击:

jquery ajax done function not firing

jquery

提问by misterrobinson

I have a VERY simple jQuery Ajax call (below). The Ajax call executes and I can see in the Firebug Net panel that the server returned 200 OK and returned a string "OK" as it should. However, the done and fail functions do not fire! Very frustrating!

我有一个非常简单的 jQuery Ajax 调用(如下)。执行 Ajax 调用,我可以在 Firebug Net 面板中看到服务器返回 200 OK 并返回字符串“OK”。但是,done 和fail 函数不会触发!非常令人沮丧!

(The "before" and "after" alerts DO fire. )

(“之前”和“之后”警报确实会触发。)

For simplicity (and as a debugging technique) I have stripped this down to it's most bare skeleton but still the handlers won't fire. What am I not seeing here?

为简单起见(并作为一种调试技术),我将其分解为最简单的骨架,但处理程序仍然不会触发。我在这里看不到什么?

postUrl= "/mod/users/check_email/";
dataToPost= { email: "[email protected]" };

alert("before");
$.ajax
({
    type: "POST", 
    url: postUrl,
    data: dataToPost,
    done: function() 
    {
        alert("Success.");
    },
    fail: function() 
    {
        alert("Sorry. Server unavailable. ");
    },
});  // end Ajax call 

alert("after");

回答by adeneo

You need to chain the done()and fail()functions, they are not part of the options object used in $.ajax:

您需要链接done()fail()函数,它们不是 中使用的选项对象的一部分$.ajax

$.ajax({
    type: "POST", 
    url : postUrl,
    data: dataToPost
}).done(function()  {
    alert("Success.");
}).fail(function()  {
    alert("Sorry. Server unavailable. ");
}); 

Also, as ajax is asynchronous, don't be suprised if the "after" alert comes before the "success" alert.

此外,由于 ajax 是异步的,如果“之后”警报出现在“成功”警报之前,请不要感到惊讶。

回答by Mustafa Gen?

successand errorcallbacks can be used in that way. For doneand fail, you need to do :

success并且error回调可以以这种方式使用。对于doneand fail,您需要执行以下操作:

$.ajax({
    type: "POST", 
    url: postUrl,
    data: dataToPost,
}).done(function() {
    alert("Success.");
}).fail(function() {
    alert("Sorry. Server unavailable. ");
});

Or :

或者 :

$.ajax({
    type: "POST", 
    url: postUrl,
    data: dataToPost,
    success: function() {
            //code here
    }
});

回答by WtFudgE

Also you need to set dataType: 'json'. I ran into this problem when I set it to 'string'and it doesn't fire the donemethod.

您还需要设置dataType: 'json'. 当我将它设置为时遇到了这个问题'string',但它没有触发该done方法。

回答by Toàn Nguy?n

I have the same setup like this:

我有这样的相同设置:

$.ajax({
    type: "POST", 
    url : postUrl,
    data: dataToPost
}).done(function()  {
    alert("Success.");
})

and ran into the same problem: the done function wouldn't fire off. Though I've managed to make it work many times before with the same set up, it took me a while to figure out what happened. After tweaking around, I found that it also depends on the type of data returned by the Ajax call. Originally, I configured the ajax call to return some HTML. The done function didn't work. After I changed it back to json (with jsonify in Flask,) it works again.

并遇到了同样的问题: done 函数不会触发。虽然我以前用相同的设置设法让它工作了很多次,但我花了一段时间才弄清楚发生了什么。折腾了一下,发现还取决于Ajax调用返回的数据类型。最初,我配置了 ajax 调用以返回一些 HTML。done 功能不起作用。在我将它改回 json(在 Flask 中使用 jsonify)之后,它又可以工作了。