Can anyone help me?
I am not able to understand the difference between success
and .done()
of $.ajax
.
谁能帮我吗?我无法理解$.ajax的success和.done()之间的区别。
If possible please give examples.
如果可能的话,请举例说明。
4 个解决方案
#1
7
In short, decoupling success callback function from the ajax function so later you can add your own handlers without modifying the original code (observer pattern).
简而言之,从ajax函数中分离成功回调函数,这样以后您就可以添加自己的处理程序,而无需修改原始代码(观察者模式)。
Please find more detailed information from here: https://*.com/a/14754681/1049184
请在这里找到更详细的信息:https://*.com/a/14754681/1049184
#2
92
success
only fires if the AJAX call is successful, i.e. ultimately returns a HTTP 200 status. error
fires if it fails and complete
when the request finishes, regardless of success.
只有在AJAX调用成功时才会触发成功,即最终返回HTTP 200状态。错误触发如果它失败了,并且在请求结束时完成,不管成功与否。
In jQuery 1.8 on the jqXHR
object (returned by $.ajax
) success
was replaced with done
, error
with fail
and complete
with always
.
在jQuery 1.8中,jqXHR对象(由$.ajax返回)的成功被替换为done, error替换为fail, complete替换为always。
However you should still be able to initialise the AJAX request with the old syntax. So these do similar things:
但是,您仍然应该能够使用旧的语法初始化AJAX请求。所以它们做着相似的事情:
// set success action before making the request
$.ajax({
url: '...',
success: function(){
alert('AJAX successful');
}
});
// set success action just after starting the request
var jqxhr = $.ajax( "..." )
.done(function() { alert("success"); });
This change is for compatibility with jQuery 1.5's deferred object. Deferred (and now Promise
, which has full native browser support in Chrome and FX) allow you to chain asynchronous actions:
此更改是为了兼容jQuery 1.5的deferred对象。延迟(现在承诺,它在Chrome和FX上有完全的本地浏览器支持)允许你链接异步操作:
$.ajax("parent").
done(function(p) { return $.ajax("child/" + p.id); }).
done(someOtherDeferredFunction).
done(function(c) { alert("success: " + c.name); });
This chain of functions is easier to maintain than a nested pyramid of callbacks you get with success
.
这个函数链比成功调用的嵌套回调金字塔更容易维护。
However, please note that done
is now deprecated in favour of the Promise
syntax that uses then
instead:
但是,请注意,done现在已被弃用,取而代之的是承诺语法:
$.ajax("parent").
then(function(p) { return $.ajax("child/" + p.id); }).
then(someOtherDeferredFunction).
then(function(c) { alert("success: " + c.name); }).
catch(function(err) { alert("error: " + err.message); });
This is worth adopting because async
and await
extend promises improved syntax (and error handling):
这值得采用,因为异步和等待扩展承诺改进语法(和错误处理):
try {
var p = await $.ajax("parent");
var x = await $.ajax("child/" + p.id);
var c = await someOtherDeferredFunction(x);
alert("success: " + c.name);
}
catch(err) {
alert("error: " + err.message);
}
#3
5
.success()
only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine.
.success()只有当您的web服务器响应一个200 OK HTTP头时才会被调用——基本上是在一切正常的情况下。
The callbacks attached to done() will be fired when the deferred is resolved. The callbacks attached to fail() will be fired when the deferred is rejected.
当解决延迟时,将触发对done()的回调。当拒绝递延时,连接到fail()的回调将被触发。
promise.done(doneCallback).fail(failCallback)
.done() has only one callback and it is the success callback
#4
0
success
is the callback that is invoked when the request is successful and is part of the $.ajax
call. done
is actually part of the jqXHR
object returned by $.ajax()
, and replaces success
in jQuery 1.8.
成功是在请求成功时调用的回调,也是$的一部分。ajax调用。done实际上是$.ajax()返回的jqXHR对象的一部分,并替换了jQuery 1.8中的success。
#1
7
In short, decoupling success callback function from the ajax function so later you can add your own handlers without modifying the original code (observer pattern).
简而言之,从ajax函数中分离成功回调函数,这样以后您就可以添加自己的处理程序,而无需修改原始代码(观察者模式)。
Please find more detailed information from here: https://*.com/a/14754681/1049184
请在这里找到更详细的信息:https://*.com/a/14754681/1049184
#2
92
success
only fires if the AJAX call is successful, i.e. ultimately returns a HTTP 200 status. error
fires if it fails and complete
when the request finishes, regardless of success.
只有在AJAX调用成功时才会触发成功,即最终返回HTTP 200状态。错误触发如果它失败了,并且在请求结束时完成,不管成功与否。
In jQuery 1.8 on the jqXHR
object (returned by $.ajax
) success
was replaced with done
, error
with fail
and complete
with always
.
在jQuery 1.8中,jqXHR对象(由$.ajax返回)的成功被替换为done, error替换为fail, complete替换为always。
However you should still be able to initialise the AJAX request with the old syntax. So these do similar things:
但是,您仍然应该能够使用旧的语法初始化AJAX请求。所以它们做着相似的事情:
// set success action before making the request
$.ajax({
url: '...',
success: function(){
alert('AJAX successful');
}
});
// set success action just after starting the request
var jqxhr = $.ajax( "..." )
.done(function() { alert("success"); });
This change is for compatibility with jQuery 1.5's deferred object. Deferred (and now Promise
, which has full native browser support in Chrome and FX) allow you to chain asynchronous actions:
此更改是为了兼容jQuery 1.5的deferred对象。延迟(现在承诺,它在Chrome和FX上有完全的本地浏览器支持)允许你链接异步操作:
$.ajax("parent").
done(function(p) { return $.ajax("child/" + p.id); }).
done(someOtherDeferredFunction).
done(function(c) { alert("success: " + c.name); });
This chain of functions is easier to maintain than a nested pyramid of callbacks you get with success
.
这个函数链比成功调用的嵌套回调金字塔更容易维护。
However, please note that done
is now deprecated in favour of the Promise
syntax that uses then
instead:
但是,请注意,done现在已被弃用,取而代之的是承诺语法:
$.ajax("parent").
then(function(p) { return $.ajax("child/" + p.id); }).
then(someOtherDeferredFunction).
then(function(c) { alert("success: " + c.name); }).
catch(function(err) { alert("error: " + err.message); });
This is worth adopting because async
and await
extend promises improved syntax (and error handling):
这值得采用,因为异步和等待扩展承诺改进语法(和错误处理):
try {
var p = await $.ajax("parent");
var x = await $.ajax("child/" + p.id);
var c = await someOtherDeferredFunction(x);
alert("success: " + c.name);
}
catch(err) {
alert("error: " + err.message);
}
#3
5
.success()
only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine.
.success()只有当您的web服务器响应一个200 OK HTTP头时才会被调用——基本上是在一切正常的情况下。
The callbacks attached to done() will be fired when the deferred is resolved. The callbacks attached to fail() will be fired when the deferred is rejected.
当解决延迟时,将触发对done()的回调。当拒绝递延时,连接到fail()的回调将被触发。
promise.done(doneCallback).fail(failCallback)
.done() has only one callback and it is the success callback
#4
0
success
is the callback that is invoked when the request is successful and is part of the $.ajax
call. done
is actually part of the jqXHR
object returned by $.ajax()
, and replaces success
in jQuery 1.8.
成功是在请求成功时调用的回调,也是$的一部分。ajax调用。done实际上是$.ajax()返回的jqXHR对象的一部分,并替换了jQuery 1.8中的success。