在jQuery的aJax方法中,传入的对象包括成功和错误处理。如果 ajax 请求参数已经可以处理这些问题,为什么您还需要使用 #then 或 #done 或 #fail 等方法呢?
最佳答案
success
和 error
不能用于传递由 $.ajax
返回的 promise 链中的 promise
您无法像在 then()
中那样向这些方法返回
任何内容。
考虑一系列 Ajax 请求,这些请求必须全部完成才能执行其他代码。
$.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');
});
此链无法使用成功回调来工作
关于jquery - 为什么需要 jQuery AJAX #then 或 #done 或 #fail 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35615872/