在前后端分离中,如何辨别用户和判断用户是否登录需要token或者cookie的验证来判断用户的时效性,将后端返回的标识数据保存到本地,每次请求后端都携带该数据以此来分别用户
https://www.cnblogs.com/monsterooo/p/6815392.html
$.ajax({
type:'post',
url:'http://127.0.0.1:5000/login',
cache: false,
data:{
'username':name,
'pwd':pwd,
}, //重点必须为一个变量如:data
contentType: "application/x-www-form-urlencoded; charset=utf-8",
success:function(data){
if(data['status']=='2'){
alert(data['errmsg'])
}if(data['status']=='1'){
alert(data['errmsg'])
}if(data['status']=='0'){
localStorage.setItem('sessions',data['session_id']); //保存到本地以sessions为key
console.log(localStorage.sessions)
window.location.href = data['errmsg'];
}
}
})
在其他网页直接localStorage.sessions
即可获取以sessions为名的data