Javascript 如何检查HTTP调用响应中的状态
若状态代码为200或响应为真,我将尝试导航到下一页。但是在response.status中没有定义。如果我只记录我得到的响应Javascript 如何检查HTTP调用响应中的状态,javascript,angular,Javascript,Angular,若状态代码为200或响应为真,我将尝试导航到下一页。但是在response.status中没有定义。如果我只记录我得到的响应 {success: true, message: "login successful"} 以下代码在控制台日志中未定义。我是初学者,不使用任何服务 goToHome() { console.log(this.login); this.Http.post('http://localhost:3000/users/login', this.login).su
{success: true, message: "login successful"}
以下代码在控制台日志中未定义。我是初学者,不使用任何服务
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'}
)获取包含响应状态代码的完整响应:
goToHome() {
this.Http.post('http://localhost:3000/users/login', this.login, {observe: 'response'})
.subscribe((response) => {
console.log(response.status);
})
}
您应该为请求添加选项,并将
observe:'response'
设置为读取完整响应
this.Http.post('http://localhost:3000/users/login', this.login, { observe: 'response' })
.subscribe((response) => {
console.log(response.status);
})
文档中的更多信息:我得到了错误,但是感谢其他答案解决了我的问题----类型为“(response:response)=>void”的参数不能分配给类型为“(value:HttpResponse)=>void”的参数。参数“响应”和“值”的类型不兼容。类型“HttpResponse”缺少类型“Response”中的以下属性:重定向、拖车、bodyUsed、arrayBuffer和4个以上。[2345]抱歉,我已从答案中删除了错误的类型定义(导致错误)。