拦截器TypeError:无法读取未定义的属性“数据”

时间:2016-06-27 04:46:39

标签: javascript angularjs angular-ui-router

我在HTTP拦截器中获得TypeError: Cannot read property 'data' of undefined

//Traceback

angular.js:11706TypeError: Cannot read property 'data' of undefined
    at q (restangular.min.js:6)
    at angular.js:13318
    at n.$eval (angular.js:14570)
    at n.$digest (angular.js:14386)
    at n.$apply (angular.js:14675)
    at l (angular.js:9725)
    at F (angular.js:9915)
    at XMLHttpRequest.C.onload (angular.js:9856)

//interceptors.js

'use strict';

app.factory('HTTP403Interceptor',
    ['$q', '$location',
      function ($q, $location) {
        var deferred = $q.defer();
        return {
          'responseError': function(response) {
            if(response.status === 403) {
              $location.path('/access/login/');
            }
            else return deferred.reject(response);
          }
        }
      }
    ]
  );

//config.router.js

    angular.module('app')
  .run(
    [ '$rootScope', '$state', '$stateParams', '$auth',
      function ($rootScope, $state, $stateParams, $auth) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        $rootScope.$on('$stateChangeStart',
          function (event, toState) {
            var loginRequired = false;
            var skipRequired = false;
            // check if this state need login
            if (toState.data && toState.data.loginRequired) {
              loginRequired = true;
            }
            // if yes and if this user is not logged in, redirect him to login page
            if (loginRequired && !$auth.isAuthenticated()) {
              event.preventDefault();
              $state.go('access.login');
            }
            // check if this state need skipping
            if (toState.data && toState.data.skipRequired) {
              skipRequired = true;
            }
            // if yes and if this user is logged in, redirect him to dashboard page
            if (skipRequired && $auth.isAuthenticated()) {
              event.preventDefault();
              $state.go('app.dashboard');
            }
          }
        );
      }
    ]
  )
  .config(
    [ '$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', '$httpProvider',
      function ($stateProvider, $urlRouterProvider, JQ_CONFIG, $httpProvider) {
        $httpProvider.interceptors.push('HTTP403Interceptor');
        $stateProvider.
        state(
          'access', {
            url: '/access',
            template: '<div ui-view class="fade-in-right-big smooth"></div>'
          }
        ).
        state(
          'access.login', {
            url: '/login/',
            templateUrl: '/tpl/auth_tpl/login.html',
            data: {skipRequired: true},
            resolve: {
              deps: ['$ocLazyLoad',
                function( $ocLazyLoad ){
                  return $ocLazyLoad.load( [
                    '/dashboard/js/controllers/auth.Controllers.js'
                  ] );
                }
              ]
            }
          }
        ).state(
          'access.lockme', {
            url: '/lockme/:email/',
            templateUrl: '/tpl/auth_tpl/lockme.html',
            data: {loginRequired: true},
            resolve: {
              deps: ['$ocLazyLoad',
                function( $ocLazyLoad){
                  return $ocLazyLoad.load('toaster').then(
                    function(){
                      return $ocLazyLoad.load('/dashboard/js/controllers/auth.Controllers.js');
                    }
                  );
                }
              ]
            }
          }
        ).state(
          'access.logout', {
            url: '/logout/',
            template: null,
            data: {loginRequired: true},
            controller: 'LogoutController',
            resolve: {
              deps: ['$ocLazyLoad',
                function( $ocLazyLoad){
                  return $ocLazyLoad.load('/dashboard/js/controllers/auth.Controllers.js');
                }
              ]
            }
          }
        ).
        state(
          'access.signup', {
            url: '/signup/',
            templateUrl: '/tpl/auth_tpl/signup.html',
            data: {skipRequired: true},
            resolve: {
              deps: ['$ocLazyLoad',
                function( $ocLazyLoad ){
                  return $ocLazyLoad.load( [
                    '/dashboard/js/controllers/auth.Controllers.js',
                    'ngIntlTelInput'
                  ] );
                }
              ]
            }
          }
        ).
        state(
          'access.verify-email', {
            url: '/verify-email/:school_id/:key/',
            templateUrl: '/tpl/auth_tpl/verifyEmail.html',
            data: {skipRequired: true},
            resolve:{
              deps: ['$ocLazyLoad',
                function ($ocLazyLoad) {
                  return $ocLazyLoad.load([
                      '/dashboard/js/controllers/auth.Controllers.js',
                      '/dashboard/js/controllers/utils.js',
                    ]
                  );
                }
              ]
            }
          }
        ).
        state(
          'access.reset-password', {
            url: '/reset/password/',
            templateUrl: '/tpl/auth_tpl/resetPassword.html',
            data: {skipRequired: true},
            resolve:{
              deps:['$ocLazyLoad',
                function ($ocLazyLoad) {
                  return $ocLazyLoad.load([
                      '/dashboard/js/controllers/auth.Controllers.js'
                    ]
                  );
                }
              ]
            }
          }
        ).
        state(
          'access.reset-password-confirm', {
            url: '/reset/password/confirm/:uid/:token/',
            templateUrl: '/tpl/auth_tpl/resetPasswordConfirm.html',
            data: {skipRequired: true},
            resolve:{
              deps:['$ocLazyLoad',
                function ($ocLazyLoad) {
                  return $ocLazyLoad.load([
                      '/dashboard/js/controllers/auth.Controllers.js'
                    ]
                  );
                }
              ]
            }
          }
        ).
        state(
          'app', {
            abstract: true,
            url: '/app',
            templateUrl: '/tpl/app.html',
            data: {loginRequired: true},
            resolve:{
              deps: ['$ocLazyLoad',
                function ($ocLazyLoad) {
                  return $ocLazyLoad.load([
                    '/dashboard/js/controllers/header.Controller.js'
                  ]);
                }
              ]
            }
          }
        ).
        state(
          'app.dashboard', {
            url: '/dashboard/',
            templateUrl: '/tpl/dashboard.html',
            resolve: {
              deps: ['$ocLazyLoad',
                function( $ocLazyLoad ){
                  return $ocLazyLoad.load([
                    '/dashboard/js/app/todo/todo.js',
                    JQ_CONFIG.moment,
                    '/dashboard/js/controllers/dashboard.Controllers.js',
                    '/dashboard/js/controllers/auth.Controllers.js'
                  ]);
                }
              ]
            }
          }
        )
        $urlRouterProvider.otherwise('/access/login/');
      }
    ]
  );

R {}
__proto__
:
Object
loginRequired
:
true
__proto__
:
Object
__defineGetter__
:
__defineGetter__()
__defineSetter__
:
__defineSetter__()
__lookupGetter__
:
__lookupGetter__()
__lookupSetter__
:
__lookupSetter__()
constructor
:
Object()
hasOwnProperty
:
hasOwnProperty()
isPrototypeOf
:
isPrototypeOf()
propertyIsEnumerable
:
propertyIsEnumerable()
toLocaleString
:
toLocaleString()
toString
:
toString()
valueOf
:
valueOf()
get __proto__
:
__proto__()
set __proto__
:
__proto__()

1 个答案:

答案 0 :(得分:0)

通过返回response

解决了这个问题
//HTTP403Interceptor to redirect user to login page on HTTP 403
app.factory('HTTP403Interceptor',
  ['$q', '$injector',
    function ($q, $injector) {
      var deferred = $q.defer();
      return {
        'responseError': function(response) {
          if(response.status === 403) {
            $injector.get('$state').go('access.logout');
            return response; // This was the problem
          }
          else return deferred.reject(response);
        }
      }
    }
  ]
);