jQuery $.ajax 的成功和 .done() 方法有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8847829/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:08:13  来源:igfitidea点击:

what is difference between success and .done() method of $.ajax

jqueryjquery-deferred

提问by Poonam Bhatt

Can anyone help me?
I am not able to understand the difference between successand .done()of $.ajax.

谁能帮我?
我无法理解之间的区别success.done()$.ajax

If possible please give examples.

如果可能,请举例说明。

采纳答案by batbaatar

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://stackoverflow.com/a/14754681/1049184

请从这里找到更多详细信息:https: //stackoverflow.com/a/14754681/1049184

回答by Keith

successonly fires if the AJAX call is successful, i.e. ultimately returns a HTTP 200 status. errorfires if it fails and completewhen the request finishes, regardless of success.

success只有在 AJAX 调用成功时才会触发,即最终返回 HTTP 200 状态。error如果失败以及complete请求完成时触发,无论成功与否。

In jQuery 1.8 on the jqXHRobject (returned by $.ajax) successwas replaced with done, errorwith failand completewith always.

在jQuery的1.8jqXHR对象(通过返回$.ajaxsuccess被替换doneerrorfailcompletealways

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(现在Promise,它在 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.

这个函数链比使用success.

However, please note that doneis now deprecated in favour of the Promisesyntax that uses theninstead:

但是,请注意,done现在已弃用,取而代之的Promise是使用以下语法then

$.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 asyncand awaitextend promises improved syntax (and error handling):

这是值得采用的,因为asyncawait扩展承诺改进了语法(和错误处理):

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); 
}

回答by Sameera Thilakasiri

.success()only gets called if your webserver responds with a 200 OK HTTP header - basically when everything is fine.

.success()仅当您的网络服务器以 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.

当 deferred 解决时,将触发附加到 done() 的回调。当延迟被拒绝时,将触发附加到 fail() 的回调。

promise.done(doneCallback).fail(failCallback)

.done() has only one callback and it is the success callback

回答by devdigital

successis the callback that is invoked when the request is successful and is part of the $.ajaxcall. doneis actually part of the jqXHRobject returned by $.ajax(), and replaces successin jQuery 1.8.

success是请求成功时调用的回调,是调用的一部分$.ajaxdone实际上是由jqXHR返回的对象的一部分$.ajax(),并success在 jQuery 1.8 中替换。