jQuery.post() .done() 和成功:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22213495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery.post( ) .done( ) and success:
提问by KaekeaSchmear
jQuery
documentation on jQuery.post( )
jQuery
文档 jQuery.post( )
// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.post( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second finished" );
});
What is the difference between the success:
parameter and the jqXHR.done( )
method; if there is none, what is the entire point of the jqXHR.done( )
method?
success:
参数和jqXHR.done( )
方法有什么区别;如果没有,该jqXHR.done( )
方法的全部意义是什么?
回答by jfriend00
jQuery used to ONLY have the callback functions for success
and error
and complete
.
jQuery 过去只有success
anderror
和 and的回调函数complete
。
Then, they decided to support promises with the jqXHR object and that's when they added .done()
, .fail()
, .always()
, etc... in the spirit of the promise API. These new methods serve much the same purpose as the callbacks but in a different form. You can use whichever API style works better for your coding style.
然后,他们决定用 jqXHR 对象支持 promises,这就是他们添加.done()
, .fail()
, .always()
, 等等的时候,本着 promise API 的精神。这些新方法与回调函数的用途大致相同,但形式不同。您可以使用更适合您的编码风格的 API 风格。
As people get more and more familiar with promises and as more and more async operations use that concept, I suspect that more and more people will move to the promise API over time, but in the meantime jQuery supports both.
随着人们对 promise 越来越熟悉,并且越来越多的异步操作使用这个概念,我怀疑随着时间的推移,越来越多的人会转向 promise API,但同时 jQuery 支持两者。
The .success()
method has been deprecated in favor of the common promise object method names.
该.success()
方法已被弃用,取而代之的是通用的 Promise 对象方法名称。
From the jQuery doc, you can see how various promise methods relate to the callback types:
从jQuery doc,您可以看到各种承诺方法与回调类型的关系:
jqXHR.done(function( data, textStatus, jqXHR ) {});An alternative construct to the success callback option, the .done() method replaces the deprecated jqXHR.success() method. Refer to deferred.done() for implementation details.
jqXHR.fail(function( jqXHR, textStatus, errorThrown ) {});An alternative construct to the error callback option, the .fail() method replaces the deprecated .error() method. Refer to deferred.fail() for implementation details.
jqXHR.always(function( data|jqXHR, textStatus, jqXHR|errorThrown ) { });An alternative construct to the complete callback option, the .always() method replaces the deprecated .complete() method.
In response to a successful request, the function's arguments are the same as those of .done(): data, textStatus, and the jqXHR object. For failed requests the arguments are the same as those of .fail(): the jqXHR object, textStatus, and errorThrown. Refer to deferred.always() for implementation details.
jqXHR.then(function( data, textStatus, jqXHR ) {}, function( jqXHR, textStatus, errorThrown ) {});Incorporates the functionality of the .done() and .fail() methods, allowing (as of jQuery 1.8) the underlying Promise to be manipulated. Refer to deferred.then() for implementation details.
jqXHR.done(function(data, textStatus, jqXHR) {}); 成功回调选项的替代构造,.done() 方法替换了已弃用的 jqXHR.success() 方法。有关实现细节,请参阅 deferred.done()。
jqXHR.fail(function(jqXHR, textStatus, errorThrown ) {}); 错误回调选项的替代构造,.fail() 方法替换了已弃用的 .error() 方法。有关实现细节,请参阅 deferred.fail()。
jqXHR.always(function(data|jqXHR,textStatus,jqXHR|errorThrown){}); 完整回调选项的替代构造, .always() 方法替换了已弃用的 .complete() 方法。
为了响应成功的请求,该函数的参数与 .done() 的参数相同:data、textStatus 和 jqXHR 对象。对于失败的请求,参数与 .fail() 的参数相同:jqXHR 对象、textStatus 和 errorThrown。有关实现细节,请参阅 deferred.always()。
jqXHR.then(function(data, textStatus, jqXHR) {}, function( jqXHR, textStatus, errorThrown ) {}); 结合了 .done() 和 .fail() 方法的功能,允许(从 jQuery 1.8 开始)操作底层 Promise。有关实现细节,请参阅 deferred.then()。
If you want to code in a way that is more compliant with the ES6 Promises standard, then of these four options you would only use .then()
.
如果您想以更符合 ES6 Promises 标准的方式进行编码,那么在这四个选项中,您只能使用.then()
.
回答by manish khandelwal
The reason to prefer Promises over callback functions is to have multiple callbacks and to avoid the problems like Callback Hell.
比回调函数更喜欢 Promises 的原因是有多个回调并避免像回调地狱这样的问题。
Callback hell (for more details, refer http://callbackhell.com/): Asynchronous javascript, or javascript that uses callbacks, is hard to get right intuitively. A lot of code ends up looking like this:
回调地狱(有关更多详细信息,请参阅http://callbackhell.com/):异步 javascript 或使用回调的 javascript 很难直观地理解。很多代码最终看起来像这样:
asyncCall(function(err, data1){
if(err) return callback(err);
anotherAsyncCall(function(err2, data2){
if(err2) return calllback(err2);
oneMoreAsyncCall(function(err3, data3){
if(err3) return callback(err3);
// are we done yet?
});
});
});
With Promises above code can be rewritten as below:
使用 Promises 上面的代码可以重写如下:
asyncCall()
.then(function(data1){
// do something...
return anotherAsyncCall();
})
.then(function(data2){
// do something...
return oneMoreAsyncCall();
})
.then(function(data3){
// the third and final async response
})
.fail(function(err) {
// handle any error resulting from any of the above calls
})
.done();
回答by royhowie
Both .done()
and .success()
are callback functions and they essentially function the same way.
两者.done()
并.success()
有回调函数,它们的功能基本上相同的方式。
Here's the documentation. The difference is that .success()
is deprecated as of jQuery 1.8. You should use .done()
instead.
这是文档。不同之处在于.success()
它从 jQuery 1.8 开始被弃用。你应该.done()
改用。
In case you don't want to click the link:
如果您不想单击链接:
Deprecation Notice
The
jqXHR.success()
,jqXHR.error()
, andjqXHR.complete()
callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, usejqXHR.done()
,jqXHR.fail()
, andjqXHR.always()
instead.
弃用通知
的
jqXHR.success()
,jqXHR.error()
和jqXHR.complete()
jQuery的1.5引入了回调方法被弃用的jQuery 1.8。要准备的代码为他们的最终消除,使用jqXHR.done()
,jqXHR.fail()
和jqXHR.always()
来代替。
回答by xdazz
从文档:
jqXHR.done(function( data, textStatus, jqXHR ) {});
An alternativeconstruct to the success callback option, the .done() method replaces the deprecatedjqXHR.success() method. Refer to deferred.done() for implementation details.
jqXHR.done(function(data, textStatus, jqXHR) {});
成功回调选项的替代构造,.done() 方法替换了已弃用的jqXHR.success() 方法。有关实现细节,请参阅 deferred.done()。
The point it is just an alternative for success callback option, and jqXHR.success()
is deprecated.
它只是成功回调选项的替代方案,jqXHR.success()
已被弃用。