无法解决 Angular 响应中的选项
Can't solve options in Angular reponse
当后端的响应为 200 时,我无法在本地存储中设置项目。似乎它无法识别请求的状态。
options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Authorization': 'Bearer '+localStorage.getItem("access_token")
}),
observe: "response", // to display the full response
responseType: "json"
};
constructor(private http: HttpClient, private router: Router) {
}
authenticate(url, data, options) {
this.http.post(environment.getBaseAddress() +url+'/', data, options).subscribe((data:any) => {
if (data.status == 200) {
localStorage.setItem("access_token", data['access_token']);
console.log(data.status)
}
else this.router.navigateByUrl('something-else');
})
}
订阅块的 next 回调被执行意味着您的 API 调用成功。所以你不需要明确地检查状态码,你也可以这样做:
this.http.post(environment.getBaseAddress() +url+'/', data, options)
.subscribe((data:any) => {
localStorage.setItem("access_token", data['access_token']);
}, (err) => {
this.router.navigateByUrl('something-else');
});
More 关于订阅回调。
当后端的响应为 200 时,我无法在本地存储中设置项目。似乎它无法识别请求的状态。
options = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Authorization': 'Bearer '+localStorage.getItem("access_token")
}),
observe: "response", // to display the full response
responseType: "json"
};
constructor(private http: HttpClient, private router: Router) {
}
authenticate(url, data, options) {
this.http.post(environment.getBaseAddress() +url+'/', data, options).subscribe((data:any) => {
if (data.status == 200) {
localStorage.setItem("access_token", data['access_token']);
console.log(data.status)
}
else this.router.navigateByUrl('something-else');
})
}
订阅块的 next 回调被执行意味着您的 API 调用成功。所以你不需要明确地检查状态码,你也可以这样做:
this.http.post(environment.getBaseAddress() +url+'/', data, options)
.subscribe((data:any) => {
localStorage.setItem("access_token", data['access_token']);
}, (err) => {
this.router.navigateByUrl('something-else');
});
More 关于订阅回调。