在Angular.js中使用sessionStorage时出错

时间:2015-10-07 11:10:48

标签: javascript angularjs session-storage

在angular.js中使用sessionStorage时出现以下错误。

  

错误:

SyntaxError: Unexpected token u
    at Object.parse (native)
    at new <anonymous> (http://localhost/Gofasto/controller/dashboardController.js:9:17)
    at e (http://localhost/Gofasto/js/angularjs.js:39:193)
    at Object.instantiate (http://localhost/Gofasto/js/angularjs.js:39:310)
    at http://localhost/Gofasto/js/angularjs.js:80:313
    at q (http://localhost/Gofasto/js/angularuirouter.js:7:14338)
    at A (http://localhost/Gofasto/js/angularuirouter.js:7:14699)
    at n.$broadcast (http://localhost/Gofasto/js/angularjs.js:135:317)
    at t.transition.N.then.t.transition.t.transition (http://localhost/Gofasto/js/angularuirouter.js:7:10000)
    at http://localhost/Gofasto/js/angularjs.js:118:334

我的目标是我有一个登录页面,其网址为http://localhost/Gofasto/,当用户成功登录时,用户将重定向到此http://localhost/Gofasto/#/dashboard网址中的仪表板页面。我正在使用sessionStorage对象在登录时存储一些值。当用户登录页面并重定向到仪表板页面时,没有错误。假设登录后用户在不知情的情况下删除了该标签,并在新标签页中再次使用直接网址http://localhost/Gofasto/#/dashboard打开页面,这些错误即将发生。我正在解释下面的代码。

  

loginController.js:

var loginAdmin=angular.module('Channabasavashwara');
loginAdmin.controller('loginController',function($scope,$http,$location,$window){
    $scope.user_name = ''; 
    $scope.user_pass = '';
    $scope.user_login=function(){
    if($scope.user_name==''){
        alert('user name filed should not keep blank');
    }else if($scope.user_pass==''){
        alert('password filed should not keep blank');
    }else{
        var userData={'user_name':$scope.user_name,'user_pass':$scope.user_pass};
        console.log('user',userData);
        $http({
            method: 'POST',
            url: "php/Login/login.php",
            data: userData,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).then(function successCallback(response){
            //console.log('response',response.data.user_name);
            alert(response.data['msg']);
            var loginData={'user_name':response.data.user_name,'pass':response.data.password,'user_type':response.data.user_type};
            $window.sessionStorage["loginInfo"]=JSON.stringify(loginData);
            //console.log('session',$window.sessionStorage["loginInfo"]);
            $location.path('dashboard');
        },function errorCallback(response) {
            alert(response.data['msg']);
        });
    }
    }
});
  

dashboardController.js:

var dashboard=angular.module('Channabasavashwara');
dashboard.controller('dashboardController',function($scope,$http,$window,$state){
    console.log('storage',$window.sessionStorage["loginInfo"]);
    //console.log('storage',data.user_type);
    if($window.sessionStorage["loginInfo"]=='undefined'){
        console.log('storage1',$window.sessionStorage["loginInfo"]);
        $state.go('/',{}, { reload: true });
    }else{
        var data=JSON.parse($window.sessionStorage["loginInfo"]);
        $scope.user_type=data.user_type;
    }
     $scope.Logout=function(){
         $window.sessionStorage.removeItem("loginInfo");
     }
})

我的要求是,一旦用户登录了一个浏览器,并且在任何时候想要直接进入仪表板页面就可以发生,但如果没有登录,如果用户想要直接进入仪表板页面i.e-http://localhost/Gofasto/#/dashboard它不会发生,用户将再次返回登录页面进行登录。请帮助我。

1 个答案:

答案 0 :(得分:0)

sessionStorage是每个标签。打开一个新标签会给出一个新鲜的空白会话。如果需要交叉表信息,您需要localStorage。