jQuery Ajax完成功能未触发 [英] jquery ajax done function not firing

查看:100
本文介绍了jQuery Ajax完成功能未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的jQuery Ajax调用(如下).执行Ajax调用,我在Firebug Net面板中看到服务器返回了200 OK,并返回了应有的字符串"OK".但是,完成和失败功能不会触发!很沮丧!

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!

(之前"和之后"警报确实会触发.)

(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: "test@not.me" };

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

alert("after");

推荐答案

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

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

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

此外,由于ajax是异步的,因此如果之后"警报位于成功"警报之前,也不要感到惊讶.

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

这篇关于jQuery Ajax完成功能未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆