jQuery的阿贾克斯()使用成功,错误和完整VS .done(),.fail()和总() [英] jQuery ajax() using success, error and complete vs .done(), .fail() and always()

查看:233
本文介绍了jQuery的阿贾克斯()使用成功,错误和完整VS .done(),.fail()和总()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的问题

  1. 如果我们改变我们的编码如以下建议?
  2. 有()之间的 .done的差异&放大器; 成功: .fail()&放大器; 错误:。总是()&放大器; 完成:

的preamble

我组建了一个jQuery.ajax电话,这是我在过去成功地做了。事情是这样的:

  $。阿贾克斯(
    {
        网址:someUrl,
        键入:POST,
        数据:someData,
        数据类型:JSON,
        成功:功能(数据){someSuccessFunction(数据); },
        错误:函数(jqXHR,textStatus,errorThrown){someErrorFunction(); }
    });
 

虽然采取了快速看一些资料,我遇到了一个参考,说明成功,错误和完整的回调pcated在jQuery 1.8代$ P $。为prepare您code为他们的最终消除,使用jqXHR.done(),jqXHR.fail(),和jqXHR.always()来代替。

我们应该因此开始编写这样的事情,而不是:

  $。阿贾克斯(使用example.php)
    .done(功能(数据){someSuccessFunction(数据);})
    .fail(功能(jqXHR,textStatus,errorThrown){someErrorFunction();})
    。总是(函数(){警报(完成);});
 

解决方案

那么有做没有任何优势,在特定的情况下。

的点 .done() .fail() 。总是( )方法是,您可以

  1. 附加多个处理程序
  2. 请打电话让任何地方,不只是当 $。阿贾克斯

如果你在 $。阿贾克斯通话网站只安装一个处理程序,然后这些优势并没有真正发挥作用。

所以,你可以返回的承诺,其他人可能附上自己的处理程序。

例子是Ajax请求后,清爽的插件:

  $。阿贾克斯prefilter(功能(OPT,origOpt,jqxhr){
    jqxhr.always(函数(){
        $([数据插件])的插件()。
    });
});
 

The questions:

  1. Should we change our coding as suggested below?
  2. Is there a difference between .done() & success:, .fail() & error: and .always() & complete:?

The preamble:

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

    $.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.

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"); });

解决方案

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

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

  1. Attach multiple handlers
  2. Do so anywhere and not just when calling $.ajax

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

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

Example is refreshing plugins after ajax request:

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

这篇关于jQuery的阿贾克斯()使用成功,错误和完整VS .done(),.fail()和总()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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