gpt4 book ai didi

ajax - Angular.js $http 请求后,无论promise成功还是失败,都调用完整的函数

转载 作者:行者123 更新时间:2023-12-04 00:03:03 25 4
gpt4 key购买 nike

我怎样才能确保complete()无论使用 Angular.js 提供的 promise API 调用 $http 的结果如何,函数都会运行吗?

$http({
method: 'POST',
url: submitUrl,
data: $scope.data
})
.success(function(data) {
// execute this code on success
})
.error(function(data) {
// execute this code on error
})
.complete(function() {
// execute this code regardless of outcome
});

一旦请求完成,人们可以使用它来隐藏 AJAX 微调器图标。无论请求结果如何,您都希望隐藏微调器。

最佳答案

我不是 Angular.js 世界上最伟大的专家,但我知道你可以这样做:

whatever.then(function() {
// success code here
}, function() {
// error code here
return true; // return anything that's not undefined (and not a `throw()`) to force the chain down the success path at the following then().
}).then(function() {
// "complete" code here
});

您基本上被迫从一个或多个 .then() 中设计出一些东西,这是 $q promise 的唯一方法。

关于ajax - Angular.js $http 请求后,无论promise成功还是失败,都调用完整的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17181508/

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