How to check http 200 status code in angular if its not available in the json response

前端 未结 3 840
抹茶落季
抹茶落季 2021-01-16 08:14

Auth Service

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

The status code doesn\'t show in the json response from t

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-16 08:49

    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:

    Service:

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

    Component:

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

    Hopefully that helps!

提交回复
热议问题