AngularJS - 当用户丢失授权注入时重定向:modulerr错误

时间:2016-11-16 17:07:43

标签: angularjs

我使用Spring Boot作为应用程序的后端。当用户在使用网站时丢失授权或后端向客户端发送401代码时,我试图进行重定向。

我在配置文件中使用拦截器:

var app = angular.module('myApp', [
    'ngRoute', 'ngResource','ngDialog', 'tableSort', 'ngMaterial', 'ngMessages', 'timer', 'config'

]).config(function ($routeProvider, $httpProvider) {

    $routeProvider
        .when('/', {
            templateUrl: 'views/main.html',
            controller: 'LoginCtrl'
        })
        .when('/main', {
            templateUrl: 'views/main.html',
            controller: 'LoginCtrl'
        })
        .when('/home', {
            templateUrl: 'views/home.html',
            controller: 'UserAccount'
        })
        .when('/search', {
            templateUrl: 'views/find_document.html',
            controller: 'findDocument'
        })
        .otherwise({redirectTo: '/'});

    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

    var interceptor = ['$rootScope', '$q', function(scope, $q) {
        function success(response) {
            return response;
        }

        function error(response) {
            var status = response.status;
            if (status == 401) {
                window.location = "/nologin"
                return;
            }
            return $q.reject(response);
        }
        return function(promise) {
            return promise.then(success, error);
        }
    }];
    $httpProvider.responseInterceptors.push(interceptor);
});

当我尝试此代码时,我在控制台日志中收到错误

错误:

angular.js:38 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.8/$injector/modulerr?p0=myApp&p1=Error%3A%2…Flocalhost%3A8080%2Fbower_components%2Fangular%2Fangular.min.js%3A21%3A179)(…)

问题出在哪里?

编辑:

来自网址的错误记录

Failed to instantiate module myApp due to:
TypeError: Cannot read property 'push' of undefined
    at http://localhost:8080/js/App.js:115:39
    at Object.invoke (http://localhost:8080/bower_components/angular/angular.min.js:41:456)
    at d (http://localhost:8080/bower_components/angular/angular.min.js:39:418)
    at http://localhost:8080/bower_components/angular/angular.min.js:40:19
    at q (http://localhost:8080/bower_components/angular/angular.min.js:7:355)
    at g (http://localhost:8080/bower_components/angular/angular.min.js:39:319)
    at cb (http://localhost:8080/bower_components/angular/angular.min.js:43:336)
    at c (http://localhost:8080/bower_components/angular/angular.min.js:20:390)
    at Bc (http://localhost:8080/bower_components/angular/angular.min.js:21:179)
    at fe (http://localhost:8080/bower_components/angular/angular.min.js:20:1

1 个答案:

答案 0 :(得分:0)

尝试查找并复制您的问题,我发现不推荐使用responseInterceptors,因为这篇文章指出:

Interceptor not working

所以你需要重构你的代码。

希望它有所帮助。