如果状态代码为 200 或响应为真,我将尝试导航到下一页。但是在 response.status 中变得不确定。如果我只记录响应,我会得到
{success: true, message: "login successful"}
以下代码在控制台日志中未定义。我是 Angular 的初学者,没有使用任何服务。
goToHome() {
console.log(this.login);
this.Http.post('http://localhost:3000/users/login', this.login).subscribe((response: Response) => {
console.log(response.status);
// if (resp.status === 200) {
// this.navCtrl.push(TabsPage);
// } else {
// const toast = this.toastController.create({
// message: 'User not found please try again',
// duration: 2000
// });
// toast.present();
// }
})
}
最佳答案
您应该为您的请求添加选项,并设置 observe: 'response'
以阅读完整的响应。
this.Http.post('http://localhost:3000/users/login', this.login, { observe: 'response' })
.subscribe((response) => {
console.log(response.status);
})
文档中的更多信息:Reading the full response
关于javascript - 如何检查来自 HTTP 调用的响应状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54361823/