Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJs-$httpProvider.interceptors多响应错误_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJs-$httpProvider.interceptors多响应错误

Javascript AngularJs-$httpProvider.interceptors多响应错误,javascript,angularjs,Javascript,Angularjs,我有401错误的响应错误处理程序。HTTP拦截器将要求模式用户进行身份验证,并继续重试HTTP请求。但问题是,如果有多个失败的请求拦截器向我显示多个模式,我需要进行几次身份验证。在第一个请求中进行身份验证后,如何重试所有HTTP请求 $httpProvider.interceptors.push(function($q, $injector, Auth) { return { responseError: function(rejection) {

我有401错误的响应错误处理程序。HTTP拦截器将要求模式用户进行身份验证,并继续重试HTTP请求。但问题是,如果有多个失败的请求拦截器向我显示多个模式,我需要进行几次身份验证。在第一个请求中进行身份验证后,如何重试所有HTTP请求

$httpProvider.interceptors.push(function($q, $injector, Auth) {
    return {
        responseError: function(rejection) {
            if (rejection.status === 401) {

                return Auth.authenticate().then(function() {
                    return $injector.get('$http')(rejection.config);
                });                    
            }

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

.factory('Auth', function($injector) {
return {
    authenticate: function() {
        var $uibModal = $injector.get('$uibModal');

        var modal = $uibModal.open({
            templateUrl: '/authenticateModal.html',
            controller: 'AuthenticateModalController',
        });

        return modal.result.then(function() {
            // success
        });
    }
}
});  



.controller('AuthenticateModalController', function ($scope, $uibModalInstance, $http, Auth) { 
$scope.submit = function() {        
    Auth.login({
          'email': $scope.email,
          'password': $scope.password
    }).then(function successCallback(response) {
        $uibModalInstance.close();
    }, function errorCallback(response) {
        console.log('error');
    });
};
})

如果对
Auth
工厂的多个调用只需要一个模态,则应定义它,以便每次都可以访问相同的模态实例:

.factory('Auth', function($injector) {
    var $uibModal = $injector.get('$uibModal');
    var modal
    return {
        authenticate: function() {
            if (!modal) {
                modal = $uibModal.open({
                    templateUrl: '/authenticateModal.html',
                    controller: 'AuthenticateModalController',
                });
            }

            return modal.result.then(function() {
                // success
            });
        }
    }
}); 

处理被拒绝的承诺和模态的代码在哪里?@tyler,模态补充道。你不需要拒绝承诺,因为如果它失败了,所有的请求都会失败,我也没关系。