jQuery.ajax处理继续响应:“成功:”还是“ .done”?
本文翻译自: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
本身并将回调函数与done
, fail
, then
或其他方法绑定。 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”?相关推荐
- ajax then fail done,我应该使用.done()和.fail()来获取新的jQuery AJAX代码而不是成功和错误...
我这样编码: $.ajax({ cache: false, url:"/Admin/Contents/GetData", data: { accountID: AccountID ...
- Ajax接收Java异常_java – 处理来自Servlet的Jquery AJAX响应中的异常
我的servlet代码是 try{ //something response.setStatus(201); out.print("Data successfully saved" ...
- ajax脚本格式,jQuery ajax调用 - jim脚本响应是什么格式?
对不起,如果这已被回答,但我搜索网络和本网站,我找不到答案...jQuery ajax调用 - jim脚本响应是什么格式? 我有一个JavaScript函数,调用jim脚本来执行各种SQLite数据库 ...
- jquery ajax xml attribute,获得jQuery ajax和asp.net webmethod xml响应工作
我有一个asp.net WebMethod,它返回一个XmlDocument对象.我可以使用jquery ajax成功调用该方法,但似乎无法使函数成功(服务器端webmethod使用正确的参数调用,但 ...
- ajax中判空函数,jQuery Ajax成功函数数据为空?
我是ajax/php的新手并且学习它.我试图通过ajax传递php值,但是我无法从php文件获取响应变量到ajax.尽管ajax成功,但为什么数据或结果或php的响应是NULL.这里有什么缺失,我无法 ...
- ajax没返回响应数据,jQuery的Ajax时无响应数据的解决方法
jQuery的Ajax时无响应数据的解决方法 复制代码 代码如下: $.ajax( { type: "POST", url: "/MemberComment.aspx/G ...
- jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法
jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 参考文章: (1)jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法 ( ...
- ajax 成功回调函数,jQuery的阿贾克斯成功回调函数定义jQuery的阿贾克斯成功回调函数定义(jQuery ajax...
我想使用jQuery的ajax从服务器获取数据. 我希望把成功的回调函数定义外面.ajax()块像下面这样. 所以,我需要声明变量dataFromServer像下面这样我就能从成功回调使用返回的数据? ...
- 不同服务器怎么响应ajax,如何从服务器获得响应而无需刷新和使用JQuery/AJAX?
是否有任何"正确"的方式来获得服务器的响应,而不使用JQuery/AJAX,当然没有刷新页面? server.js:如何从服务器获得响应而无需刷新和使用JQuery/AJAX? v ...
最新文章
- 推荐8个舍不得分享的实用软件和网站,解决很多需求
- windows 安装cython-bbox
- webpack4.0 babel配置遇到的问题
- NDVI等植被相关指数
- MyBatis教程– CRUD操作和映射关系–第1部分
- 史上最全Spring面试71题与答案
- 斗鱼上市进行时:将登陆纳斯达克 发行价区间定为11.5至14美元
- cocos2d-x之使用plist文件初试
- ubuntu 英伟达显卡驱动异常
- 多个ajax执行混乱问题
- Bash之正则表达式
- 桌面文件删除不掉的解决方案
- 获取高匿代理ip的想法思路
- 在Maven项目中使用tk-mybatis(不结合SpringBoot)
- Linux创建用户和删除用户
- 诗琳通:中泰友谊使者,曾在汶川地震时曾为汶川灾区捐款1100万
- android绿豆通讯录xml,Android 数据库(SQLite)【简介、创建、使用(增删改查、事务、实战演练)、数据显示控件(ListView、Adapter、实战演练)】...
- 嵌入式ARM下使用ALSA USB声卡
- html5 图片局部马赛克,javascript - JS实现马赛克图片效果完整示例
- 三维点云:PCA(下)open3d