gpt4 book ai didi

javascript - 干净地链接 jQuery ajax 请求

转载 作者:行者123 更新时间:2023-12-02 14:42:57 25 4
gpt4 key购买 nike

什么是链接 jQuery ajax 请求的干净且可读的方法?我正在努力摆脱这个回调 hell 。我已经准备了很多关于这个问题的帖子,但他们似乎没有在没有更复杂的情况下回答这个问题。

$.get('file1.php')
.done(function(data) {
$.get('file2.php')
.done(function(data) {
$.get('file3.php')
.done(function(data) {

})
.fail(function() {
showError();
})
})
.fail(function() {
showError();
})
})
.fail(function() {
showError();
})

最佳答案

请注意,jQuery ajax 请求返回的 jqXHR 对象可以与 then 链接起来。 :

$.get('file1.php').then(function () {
return $.get('file2.php');
}, showError).then(function () {
return $.get('file3.php');
}, showError).then(function ()
...
}, showError);

关于javascript - 干净地链接 jQuery ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com