繁体   English   中英

如何检查HTTP调用的响应状态

[英]How to check status in response from HTTP call

如果状态代码为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选项( {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);
    })

文档中的更多信息: 阅读完整的回复

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM