本文翻译自:jQuery.ajax handling continue responses: “success:” vs “.done”?

I have been working with jQuery and AJAX for a few weeks now and I saw two different ways to 'continue' the script once the call has been made: success: and .done . 我已经使用jQuery和AJAX几周了,并且在调用完成后,我看到了两种“继续”脚本的不同方式: success:.done

From the synopsis from the jQuery documentation we get: 从jQuery文档的提要中,我们得到:

.done(): Description: Add handlers to be called when the Deferred object is resolved. .done():说明:添加要解析Deferred对象时要调用的处理程序。

success: (.ajax() option): A function to be called if the request succeeds. 成功:(。ajax()选项):如果请求成功,则要调用的函数。

So, both do something after the AJAX call has been completed/resolved. 因此,在AJAX调用完成/解决之后,两者都要做一些事情。 Can I use one or the other randomly? 我可以随机使用其中之一吗? What is the difference and when one is used instead of the other? 有什么区别?何时使用一种替代另一种?


#1楼

参考:https://stackoom.com/question/b5kn/jQuery-ajax处理继续响应-成功-还是-done


#2楼

If you need async: false in your ajax, you should use success instead of .done . 如果需要在ajax中使用async: false ,则应使用success而不是.done Else you better to use .done . 否则,您最好使用.done This is from jQuery official site : 这是从jQuery官方网站 :

As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; 从jQuery 1.8开始,不建议使用async:false和jqXHR($ .Deferred); you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() . 必须使用成功/错误/完成回调选项, 而不要使用jqXHR对象的相应方法,例如jqXHR.done()


#3楼

success has been the traditional name of the success callback in jQuery, defined as an option in the ajax call. success一直是jQuery中成功回调的传统名称,定义为ajax调用中的一个选项。 However, since the implementation of $.Deferreds and more sophisticated callbacks, done is the preferred way to implement success callbacks, as it can be called on any deferred . 但是,由于$.Deferreds和更复杂的回调的实现, done是实现成功回调的首选方法,因为可以在任何deferred上调用它。

For example, success: 例如,成功:

$.ajax({url: '/',success: function(data) {}
});

For example, done: 例如,完成:

$.ajax({url: '/'}).done(function(data) {});

The nice thing about done is that the return value of $.ajax is now a deferred promise that can be bound to anywhere else in your application. done$.ajax是, $.ajax的返回值现在是一个递延的承诺,可以将其绑定到应用程序中的任何其他位置。 So let's say you want to make this ajax call from a few different places. 假设您要在几个不同的地方进行此ajax调用。 Rather than passing in your success function as an option to the function that makes this ajax call, you can just have the function return $.ajax itself and bind your callbacks with done , fail , then , or whatever. 与其传递成功函数作为进行此Ajax调用的函数的选项,还不如让函数返回$.ajax本身并将回调函数与donefailthen或其他方法绑定。 Note that always is a callback that will run whether the request succeeds or fails. 请注意, always是一个无论请求成功还是失败都将运行的回调。 done will only be triggered on success. done只会在成功时触发。

For example: 例如:

function xhr_get(url) {return $.ajax({url: url,type: 'get',dataType: 'json',beforeSend: showLoadingImgFn}).always(function() {// remove loading image maybe}).fail(function() {// handle request failures});}xhr_get('/index').done(function(data) {// do stuff with index data
});xhr_get('/id').done(function(data) {// do stuff with id data
});

An important benefit of this in terms of maintainability is that you've wrapped your ajax mechanism in an application-specific function. 就可维护性而言,这样做的一个重要好处是,您已经将ajax机制包装在了特定于应用程序的功能中。 If you decide you need your $.ajax call to operate differently in the future, or you use a different ajax method, or you move away from jQuery, you only have to change the xhr_get definition (being sure to return a promise or at least a done method, in the case of the example above). 如果您决定以后需要$.ajax调用来以其他方式操作,或者使用其他ajax方法,或者退出jQuery,则只需更改xhr_get定义(请确保返回promise或至少返回一个done方法,在上面的例子中)的情况下。 All the other references throughout the app can remain the same. 整个应用程序中的所有其他参考都可以保持不变。

There are many more (much cooler) things you can do with $.Deferred , one of which is to use pipe to trigger a failure on an error reported by the server, even when the $.ajax request itself succeeds. $.Deferred可以执行更多(更酷)的操作,其中之一是使用pipe在服务器报告的错误上触发失败,即使$.ajax请求本身成功也是如此。 For example: 例如:

function xhr_get(url) {return $.ajax({url: url,type: 'get',dataType: 'json'}).pipe(function(data) {return data.responseCode != 200 ?$.Deferred().reject( data ) :data;}).fail(function(data) {if ( data.responseCode )console.log( data.responseCode );});
}xhr_get('/index').done(function(data) {// will not run if json returned from ajax has responseCode other than 200
});

Read more about $.Deferred here: http://api.jquery.com/category/deferred-object/ 在此处阅读有关$.Deferred更多信息: http : $.Deferred

NOTE : As of jQuery 1.8, pipe has been deprecated in favor of using then in exactly the same way. 注意 :从jQuery 1.8开始,不赞成使用pipe ,而是以完全相同的方式使用then

jQuery.ajax处理继续响应:“成功:”还是“ .done”?相关推荐

  1. ajax then fail done,我应该使用.done()和.fail()来获取新的jQuery AJAX代码而不是成功和错误...

    我这样编码: $.ajax({ cache: false, url:"/Admin/Contents/GetData", data: { accountID: AccountID ...

  2. Ajax接收Java异常_java – 处理来自Servlet的Jquery AJAX响应中的异常

    我的servlet代码是 try{ //something response.setStatus(201); out.print("Data successfully saved" ...

  3. ajax脚本格式,jQuery ajax调用 - jim脚本响应是什么格式?

    对不起,如果这已被回答,但我搜索网络和本网站,我找不到答案...jQuery ajax调用 - jim脚本响应是什么格式? 我有一个JavaScript函数,调用jim脚本来执行各种SQLite数据库 ...

  4. jquery ajax xml attribute,获得jQuery ajax和asp.net webmethod xml响应工作

    我有一个asp.net WebMethod,它返回一个XmlDocument对象.我可以使用jquery ajax成功调用该方法,但似乎无法使函数成功(服务器端webmethod使用正确的参数调用,但 ...

  5. ajax中判空函数,jQuery Ajax成功函数数据为空?

    我是ajax/php的新手并且学习它.我试图通过ajax传递php值,但是我无法从php文件获取响应变量到ajax.尽管ajax成功,但为什么数据或结果或php的响应是NULL.这里有什么缺失,我无法 ...

  6. ajax没返回响应数据,jQuery的Ajax时无响应数据的解决方法

    jQuery的Ajax时无响应数据的解决方法 复制代码 代码如下: $.ajax( { type: "POST", url: "/MemberComment.aspx/G ...

  7. jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法

    jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 参考文章: (1)jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 ( ...

  8. ajax 成功回调函数,jQuery的阿贾克斯成功回调函数定义jQuery的阿贾克斯成功回调函数定义(jQuery ajax...

    我想使用jQuery的ajax从服务器获取数据. 我希望把成功的回调函数定义外面.ajax()块像下面这样. 所以,我需要声明变量dataFromServer像下面这样我就能从成功回调使用返回的数据? ...

  9. 不同服务器怎么响应ajax,如何从服务器获得响应而无需刷新和使用JQuery/AJAX?

    是否有任何"正确"的方式来获得服务器的响应,而不使用JQuery/AJAX,当然没有刷新页面? server.js:如何从服务器获得响应而无需刷新和使用JQuery/AJAX? v ...

最新文章

  1. 推荐8个舍不得分享的实用软件和网站,解决很多需求
  2. windows 安装cython-bbox
  3. webpack4.0 babel配置遇到的问题
  4. NDVI等植被相关指数
  5. MyBatis教程– CRUD操作和映射关系–第1部分
  6. 史上最全Spring面试71题与答案
  7. 斗鱼上市进行时:将登陆纳斯达克 发行价区间定为11.5至14美元
  8. cocos2d-x之使用plist文件初试
  9. ubuntu 英伟达显卡驱动异常
  10. 多个ajax执行混乱问题
  11. Bash之正则表达式
  12. 桌面文件删除不掉的解决方案
  13. 获取高匿代理ip的想法思路
  14. 在Maven项目中使用tk-mybatis(不结合SpringBoot)
  15. Linux创建用户和删除用户
  16. 诗琳通:中泰友谊使者,曾在汶川地震时曾为汶川灾区捐款1100万
  17. android绿豆通讯录xml,Android 数据库(SQLite)【简介、创建、使用(增删改查、事务、实战演练)、数据显示控件(ListView、Adapter、实战演练)】...
  18. 嵌入式ARM下使用ALSA USB声卡
  19. html5 图片局部马赛克,javascript - JS实现马赛克图片效果完整示例
  20. 三维点云:PCA(下)open3d

热门文章

  1. 【工具】开发环境之vagrant
  2. Mybatis(四) 高级映射,一对一,一对多,多对多映射
  3. 使用 ext3grep 恢复数据试验成功 笔记
  4. 网页素材大宝库:20套精美的清爽风格图标素材
  5. 编译SOCI-3.1.0 开启sqlite3支持
  6. moosefs-1.6.10 安装手记
  7. 今天我才明白了泛型,泛型类 泛型方法
  8. 创建一个新的extender
  9. SwiftTour基础学习(五)控制流
  10. 职场白骨精必看的五个寓言