jQuery ajax()使用success、error和complete vs .done()、.fail()和always()

时间:2022-09-15 12:06:18

The questions:

的问题:

  1. Should we change our coding as suggested below?
  2. 我们是否应该像下面建议的那样修改我们的代码?
  3. Is there a difference between .done() & success:, .fail() & error: and .always() & complete:?
  4. 在。done()和success:, .fail()和error:和.always()和complete:之间有区别吗?

The preamble:

序言:

I was putting together a jQuery.ajax call, which I have done successfully in the past too. Something like this:

我在制作一个jQuery。ajax调用,我在过去也成功地完成了它。是这样的:

    $.ajax(
    {
        url: someUrl,
        type: 'POST',
        data: someData,
        datatype: 'json',
        success: function (data) { someSuccessFunction(data); },
        error: function (jqXHR, textStatus, errorThrown) { someErrorFunction(); }
    });

While taking a quick look at some documentation, I came across a reference stating that The success, error and complete callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

在快速查看一些文档时,我遇到了一个引用,说明在jQuery 1.8中不赞成成功、错误和完全回调。为了准备最终删除的代码,请使用jqXHR.done()、jqXHR.fail()和jqXHR.always()。

We should therefore start coding something like this instead:

因此,我们应该开始编写这样的代码:

$.ajax( "example.php" )
    .done(function (data) { someSuccessFunction(data); })
    .fail(function (jqXHR, textStatus, errorThrown) { someErrorFunction(); })
    .always(function() { alert("complete"); });

1 个解决方案

#1


34  

Well there is no advantage of doing that in that particular situation.

在这种情况下这样做没有什么好处。

The point of the .done() .fail() .always() methods is that you can

.done() .fail() .always()方法的要点是可以

  1. Attach multiple handlers
  2. 将多个处理程序
  3. Do so anywhere and not just when calling $.ajax
  4. 在任何地方都这样做,而不仅仅是在调用$.ajax时

If you are at the $.ajax call site only attaching single handlers then those advantages don't really come into play.

如果你在$。ajax调用站点只附加一个处理程序,这样这些优势就不会真正发挥作用。

So you can return the promise and others may attach their own handlers.

因此,您可以返回承诺,其他人可以附加自己的处理程序。

Example is refreshing plugins after ajax request:

例如,在ajax请求后刷新插件:

$.ajaxPrefilter(function(opt, origOpt, jqxhr) {
    jqxhr.always(function() {
        $("[data-plugin]").plugin();
    });
});

#1


34  

Well there is no advantage of doing that in that particular situation.

在这种情况下这样做没有什么好处。

The point of the .done() .fail() .always() methods is that you can

.done() .fail() .always()方法的要点是可以

  1. Attach multiple handlers
  2. 将多个处理程序
  3. Do so anywhere and not just when calling $.ajax
  4. 在任何地方都这样做,而不仅仅是在调用$.ajax时

If you are at the $.ajax call site only attaching single handlers then those advantages don't really come into play.

如果你在$。ajax调用站点只附加一个处理程序,这样这些优势就不会真正发挥作用。

So you can return the promise and others may attach their own handlers.

因此,您可以返回承诺,其他人可以附加自己的处理程序。

Example is refreshing plugins after ajax request:

例如,在ajax请求后刷新插件:

$.ajaxPrefilter(function(opt, origOpt, jqxhr) {
    jqxhr.always(function() {
        $("[data-plugin]").plugin();
    });
});