gpt4 book ai didi

javascript - 在 Angular.js 中使用 sessionStorage 时出现错误

转载 作者:行者123 更新时间:2023-11-28 06:50:34 25 4
gpt4 key购买 nike

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

Error:

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 url。我使用 sessionStorage 对象在登录时存储一些值。当用户登录页面并重定向到仪表板页面时,没有错误。假设登录后,用户在不知情的情况下删除了该选项卡,然后在新选项卡中使用直接 url 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");
}
})

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

最佳答案

sessionStorage 是针对每个选项卡的。打开一个新选项卡会出现一个全新的空白 sessionStorage。如果您需要交叉表信息,则需要 localStorage。

关于javascript - 在 Angular.js 中使用 sessionStorage 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32990672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com