jQuery.ajax处理继续响应:"成功:" VS" .done"?
我一直在与jQuery和AJAX了几个星期了,我看到了两种不同的方式继续剧本一旦呼叫已经取得:成功:
和 .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的文档的大纲我们得到:
.done():说明:添加处理程序时,递延对象是解决被称为
.done(): Description: Add handlers to be called when the Deferred object is resolved.
成功:(阿贾克斯()选项):一个函数,如果请求成功,被称为
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. Can I use one or the other randomly? What is the difference and when one is used instead of the other?
成功
已成功回调的jQuery的传统名称,定义为AJAX选项呼叫。然而,由于 $的执行情况。Deferreds
和更复杂的回调,完成
是preferred实施方式成功的回调,因为它可以被称为在任何延迟
。
success
has been the traditional name of the success callback in jQuery, defined as an option in the ajax call. 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
.
例如,成功的:
$.ajax({
url: '/',
success: function(data) {}
});
例如,做:
$.ajax({url: '/'}).done(function(data) {});
有关的好处完成
是 $。阿贾克斯
的返回值现在是一个延期的承诺,可以绑定到应用程序中的任何地方都多。因此,让我们说,你想从几个不同的地方这Ajax调用。而不是将你的成功的功能作为一个选项的功能,使这个Ajax调用,你可以有函数返回 $。阿贾克斯
本身并绑定您的回调与完成
,失败
,然后
,或什么的。需要注意的是总是
是一个回调将运行该请求是否成功或失败。 完成
将只被成功触发。
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. So let's say you want to make this ajax call from a few different places. 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. Note that always
is a callback that will run whether the request succeeds or fails. done
will only be triggered on success.
例如:
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
});
这个,从可维护性方面的一个重要的好处是,你已经包裹你的应用程序中的特定功能的AJAX机制。如果你决定你需要你的 $。阿贾克斯
呼吁在未来不同的操作,或者使用不同的AJAX方法,或者你远离jQuery的,你只需要改变在 xhr_get
定义(即一定要返回一个承诺或至少完成
方法,在本例的情况下,上述)。所有整个该应用的其它参考文献可以保持不变。
An important benefit of this in terms of maintainability is that you've wrapped your ajax mechanism in an application-specific function. 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). 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. 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
});
了解更多关于 $递延
这里:http://api.jquery.com/category/deferred-object/
注意:对于jQuery 1.8,管道
有pcated使用有利于被撤销$ P $ 然后
中完全相同的方式
NOTE: As of jQuery 1.8, pipe
has been deprecated in favor of using then
in exactly the same way.