我使用api创建了一个http post请求,现在我遇到了一个小问题。一旦我注销表单,我仍然可以访问主页,而不需要输入登录详细信息。即使在注销后,会话也不会过期。下面是我在登录控制器中使用的代码:
var app = angular.module('LoginApp');
app.controller('loginController', function($scope,$http,$rootScope,$window) {
$scope.postdata = function () {
var post = $http({
method: "POST",
url: "https://api-stg.martcart.pk/api/v1/user/login",
dataType: 'json',
data: JSON.stringify({
userName: $scope.username,
password: $scope.password
}),
headers: { "Content-Type": "application/json" }
});
post.success(function (data) {
window.location='components/home/home.html';
sessionStorage.setItem("authdata",JSON.stringify(data.token));
});
post.error(function () {
alert("wrong credentials");
});
}
$scope.logout = function(){
// console.log("hello")
window.location.href = 'http://localhost:5500/LoginAngularJS-API-master/index.html#/login';
$window.sessionStorage.removeItem("authdata");
//localStorage.removeItem("authdata");
// $route.reload();
// delete $window.sessionStorage;
// $window.localStorage.clear();
}
});
单击注销按钮后,将调用logout.js函数:
function logout() {
window.location.href = 'http://127.0.0.1:5500/LoginAngularJS-API-master/index.html#/login';
sessionStorage.removeItem("authdata");
// sessionStorage.destroy();
// localStorage.removeItem("authdata");
// delete $window.sessionStorage;
// $window.localStorage.clear();
}
如果有人能帮我们解决这个问题,我们将非常高兴。非常感谢
发布于 2021-07-12 21:10:35
您的代码看起来很好。也许可以试试sessionStorage.removeItem("authdata");
没有$window的话。
https://stackoverflow.com/questions/68346262
复制相似问题