如果JSON响应中不可用,如何检查Angular中的http 200状态代码 [英] How to check http 200 status code in angular if its not available in the json response

查看:111
本文介绍了如果JSON响应中不可用,如何检查Angular中的http 200状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

身份验证服务

 logout() {
      return this.http.post(this.logOutApi, null);
    }

状态代码未在后端的json响应中显示,但在邮递员的状态上显示.如何获取状态码.

The status code doesn't show in the json response from the back end but is shows on postman's status. How to get the status code.

ts文件

logout() {
    this.chk.logout().subscribe((res: any)=>{
      if(res.status == 200) //doesnt work{
      console.log(res);
      })
      }
    }, (err)=>{
      alert("There was a problem logging you out");
    });
}

推荐答案

您可以使用{ observe: 'response' }选项来读取完整的响应,包括成功处理程序中的状态代码.这将使您可以访问类型为 HttpResponse

You can use the option of { observe: 'response' } to read the full response including the status code in the success handler. This would give you access to a response of type HttpResponse:

服务:

logout() {
  // you should consider providing a type
  return this.http.post(this.logOutApi, null, { observe: 'response' });
}

组件:

logout() {
    this.chk.logout().subscribe(
      (res) => {
        if (res.status == 200) {
          console.log(res);
        })
      }
    }, (err) => {
      alert("There was a problem logging you out");
    });
}

希望有帮助!

这篇关于如果JSON响应中不可用,如何检查Angular中的http 200状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆