我正在使用带有Java 8和SQL Server 2014的WildFly 10的AngularJS 1.6.4。我可以获得登录更正,但是当我刷新页面时,同样不能保持会话。
可能会发生什么?除了我在'user.service.js'中做的调整之外,我是否还需要更改其他任何内容才能保持会话?
我的代码user.service.js
(function () {
'use strict';
angular
.module('app')
.factory('UserService', UserService);
UserService.$inject = ['$http'];
function UserService($http) {
var service = {};
//service.GetAll = GetAll;
//service.GetById = GetById;
service.GetByUsername = GetByUsername;
service.Create = Create;
//service.Update = Update;
//service.Delete = Delete;
return service;
//function GetAll() {
// return $http.get('rest/usuarios/login/').then(handleSuccess, handleError('Error getting all users'));
//}
//function GetById(id) {
// return $http.get('rest/usuarios/login/' + id).then(handleSuccess, handleError('Error getting user by id'));
//}
function GetByUsername(usermatricula, usersenha) {
return $http.post('rest/usuarios/login/' + usermatricula + '/' + usersenha).then(handleSuccess, handleError('Error getting user by username'));
}
function Create(user) {
return $http.post('rest/usuarios/', user).then(handleSuccess, handleError('Error creating user'));
}
//function Update(user) {
// return $http.put('rest/usuarios/login/' + user.id, user).then(handleSuccess, handleError('Error updating user'));
//}
//function Delete(id) {
// return $http.delete('rest/usuarios/login/' + id).then(handleSuccess, handleError('Error deleting user'));
//}
// private functions
function handleSuccess(res) {
console.log(res.data);
return res.data;
}
function handleError(error) {
return function () {
return { success: false, message: error };
};
}
}
})();
答案 0 :(得分:0)
当您从请求获得响应时,您可以将用户数据存储在localStorage中,如下所示
function handleSuccess(res) {
console.log(res.data);
// Put the object into storage
localStorage.setItem('userSession', JSON.stringify(res.data));
return res.data;
}
要检查会话是否存在,您可以执行
// If an object named userSession exists in the local storage
if(localStorage.getItem('userSession')){
// Retrieve the object from storage
var userSession = JSON.parse(localStorage.getItem('userSession'));
}
然后检查userSession中的数据