我有以下方法:
public login(username: string, password: string): Observable<any> {
console.log('Sending the login credentials to obtain a token');
const credentials = { 'email' : username, 'password' : password };
const url: string = environment.USER_REST_URL + '/login';
return this.httpClient.post<any>(url, credentials);
}
login(): void {
// this.keycloakClientService.login(this.username, this.password).subscribe(
this.userRestService.login(this.username, this.password).subscribe(
data => {
this.authService.setJwtTokenToLocalStorage(data.token);
this.router.navigate(['user']);
},
error => {
console.log(error);
}
);
}
但是数据是空的。但是,服务器返回了一个响应。
这个
error
块未执行操作,流进入
data
阻止。
6.0.4
.
更新:
错误更简单,而且完全是我的:服务器响应是空的,因为它的内容只是响应中设置的令牌
Authorization
标题。
下面是如何将响应头包含在响应对象中
const credentials = { 'email' : username, 'password' : password };
// Have the response headers included in the response object
const options = {
observe: 'response' as 'body'
};
const url: string = environment.USER_REST_URL + '/login';
return this.httpClient.post<any>(url, credentials, options);
以及如何检索标题内容
this.userRestService.login(this.username, this.password).subscribe(
response => {
const token = response.headers.get('Authorization');
this.authService.setJwtTokenToLocalStorage(token);
this.router.navigate(['user']);
},
error => {
console.log(error);
}
);