【问题标题】:Waiting for modal promise in Angular error interceptor在 Angular 错误拦截器中等待模态承诺
【发布时间】:2017-02-06 12:21:35
【问题描述】:

我正在使用承诺从我的网络应用程序中查询服务。如果令牌无效,我现在想使用模式向用户询问凭据,从后端查询新令牌,重新运行失败的请求并返回我初始请求的成功块以继续,因为什么都没发生.

所以我设置了一个错误拦截器来捕获 401,并打开一个请求凭据的模式。然后我使用现有的 AuthService 查询一个令牌并返回 $http(response.config) 和调整后的令牌。

这是我最初的要求:

MyService.getData().then(
    function(result) {
        console.log("process data");
    }
    , function(response) {
        console.log("error occurred retrieving data");
    }
);

这是我的错误拦截器,对 401 做出反应:

Restangular.setErrorInterceptor(
        function(response) {
            if (response.status === 401) {
                console.log("interceptor catches");

                var modal = $uibModal.open({
                    templateUrl: "frame/view/Relogin.html",
                    controller: "ReloginController",
                });

                console.log("opened modal");

                return modal.result.then(function(data) {
                    console.log("get new token");
                    return AuthService.authenticate(data[0], data[1]).then(
                        function (token) {
                            console.log("set new token");                                
                        },
                        function(){
                            console.log("failed getting new token");
                        })
                        .then(function() {
                            console.log("now rerun request");
                            return $http(response.config);
                        });
                });
            }

            return $q.reject(response);
        });

现在发生的是,拦截器捕获 401,并打开模式。但是现在不是等待用户输入并在后端查询新令牌,而是现在访问承诺的错误块。

所以控制台输出是:

interceptor catches
opened modal
error occurred retrieving data

get new token
set new token
now rerun request

但我希望它是:

interceptor catches
opened modal
get new token
set new token
now rerun request
process data

我认为使用 Promise 有一个错误...有人可以帮忙吗?

谢谢!

【问题讨论】:

    标签: angularjs promise bootstrap-modal angular-promise


    【解决方案1】:

    看起来我混淆了 Restangular 和 Angular。按照here的例子,我终于找到了问题所在。在 >Angular

    app.config(function($httpProvider){
        $httpProvider.interceptors.push(function($q, $injector) {
            return {
                responseError: function(response) {
                    if (response.status === 401) {
                        console.log("interceptor catches");
    
                        var modal = $injector.get('$uibModal').open({
                            templateUrl: "frame/view/Relogin.html",
                            controller: "ReloginController",
                        });
    
                        return modal.result.then(function(data) {
    
                            return $injector.get('AuthService').authenticate(data[0], data[1]).then(
                                function (token) {
                                    console.log("set new token");                                
                                },
                                function(){
                                    console.log("failed getting new token");
                                })
                        .then(function() {
                            console.log("now rerun request");
                            return $injector.get('$http')(response.config);
                        });
                });
            }
    
            return $q.reject(response);
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      • 2018-01-17
      • 1970-01-01
      • 2019-10-15
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多