一周内两次,我对一个问题开出悬赏状然后回答,我能做什么?我不能删除它。
所以我需要更改我的登录功能
login(username, password) {
const data = {
username: username,
password: password
};
const headers = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post(this.login_url, data, { headers: headers, observe: 'response' });
}
这就是你如何得到x-会话
onLogin() {
this.auth.login(this.username, this.password).subscribe(data => {
this.auth.setLoggedIn(true);
localStorage.setItem('login', JSON.stringify(this.auth.isLoggedIn));
if (localStorage.getItem('data') === null) {
localStorage.setItem('data', JSON.stringify(data));
}
const session = data.headers.get('x-session');
const expiry = data.headers.get('x-session-expiry');
localStorage.setItem('session', JSON.stringify(session));
localStorage.setItem('session-expiry', JSON.stringify(expiry));
this.router.navigate(['']);
}, err => {
console.log(err);
});
}
这就是你使用它的方式,例如
getUsers
从上面
getUsers() {
const session = JSON.parse(localStorage.getItem('session'));
if (session != null) {
let headers = new HttpHeaders().set('Content-Type', 'application/json');
headers = headers.set('x-session', session);
return this.http.get(this.users_url, { headers: headers });
}
}