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

前端 未结 3 811
抹茶落季
抹茶落季 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:30

    Other than 200 you get the status code in your error block. So here you will have to handle accordingly like below

     logout() {
            this.chk.logout().subscribe((res: any)=>{
              if(res.status == 200) //doesnt work{
              console.log(res);
              })
              }
            }, (error)=>{
              if (error.status === 500) {
                    alert('Server down please try after some time');
              }
              else if (error.status === 404) {
                   alert('Server down. Please try after some time');
             }
    
            });
        } 
    

    Hope this help

    0 讨论(0)
  • 2021-01-16 08:45

    It comes under error callback

    , (err)=>{
      alert(err.status);
    });
    
    0 讨论(0)
  • 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!

    0 讨论(0)
提交回复
热议问题