你为什么需要jQuery AJAX #then或#done或#fail方法?

时间:2016-02-25 00:17:45

标签: jquery ajax

在jQuery的aJax方法中,传入的对象包括成功和错误处理。那么为什么你仍然需要使用像#then或#done或#fail这样的方法,如果它已经在ajax请求参数中已经处理好了呢?

1 个答案:

答案 0 :(得分:3)

successerror无法用于传递$.ajax

返回的承诺链中的承诺

return可以then()$.getJSON(url) .then(function(resp1) { // this request won't run until previous one completed return $.getJSON(resp1.urlValue).done(function(resp2) { // can do things in individual request done also }); }).then(function(resp2) { return $.getJSON(resp2.urlValue, function(resp3){ // or do something in success callback for this request }); }).then(function() { // do something here now that all the requests have resolved }).fail(function() { alert('I fire if any of the above fail'); }); 的方式{{1}} {/ 1}}。

考虑一系列ajax请求,这些请求必须在其他代码执行之前完成。

{{1}}

这个链条使用成功回调

不会起作用
相关问题