gpt4 book ai didi

angularjs - 在拦截器中重试 Retangular 调用

转载 作者:行者123 更新时间:2023-12-04 14:58:11 24 4
gpt4 key购买 nike

官方 retangular 文档 provides此代码示例在 ErrorInterceptor 中重试请求:

var refreshAccesstoken = function() {
var deferred = $q.defer();

// Refresh access-token logic

return deferred.promise;
};

Restangular.setErrorInterceptor(function(response, deferred, responseHandler) {
if(response.status === 403) {
refreshAccesstoken().then(function() {
// Repeat the request and then call the handlers the usual way.
$http(response.config).then(responseHandler, deferred.reject);
// Be aware that no request interceptors are called this way.
});

return false; // error handled
}

return true; // error not handled
});

但是,正如评论中所说,第二次尝试不会触发任何拦截器,因为它直接使用 $http 。

有没有办法让第二个请求也通过 Retangular 管道并执行 ErrorInterceptor?

最佳答案

实际上,使用 Restangular 发出的请求使用了被设计为只解决一次的 promise 。如果被拒绝,您将无法重新发送。

我真的不知道你是否想要一个可重复使用的 errorInterceptor与否,但该服务的工作方式似乎不允许仅从 response object config 重新构建 Retangular 对象。 .

您可以做的一件事是保存对您的对象的引用,然后在拦截器中使用它。

你应该有你的理由,但我必须警告你这一点,因为万一失败,如果它持续存在,你最终可能会无限调用,因为 Retangular 将始终调用 errorInterceptor .

我模拟了一点 Plunkr对于演示,只需打开您的网络选项卡并单击按钮,您应该会看到所有请求都通过那里。

关于angularjs - 在拦截器中重试 Retangular 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25725896/

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