3

I want to get the StatusCode value from the post request in order to use it in my component. This is what I have done:
Api call:

  Login(user: User) {
    return this.http.post(apiUrl + 'account/Login', user).subscribe();
  }

Method in component:

  Login() {
    this.user.UserName = this.loginForm.controls.userName.value;
    this.user.Password = this.loginForm.controls.password.value;

    this.api.Login(this.user)
  }

Now it is displayed only as error enter image description here The result should be like: enter image description here

Update
It's not the cors issue...
Success login: enter image description here

Storm
  • 557
  • 1
  • 8
  • 25

1 Answers1

5

Add an error callback in your subscribe to catch the errors from the HTTP Observable.

Login(user: User) {
    return this.http.post(apiUrl + 'account/Login', user).subscribe(
        (data) => {

        },
        (error) => {
           console.log(error);
           // get the status as error.status
        })
}

In case you want to get all the status code, irrespective of success or failure, you have to observe for response in your request.

Make your API call like:

this.http.post(apiUrl + 'account/Login', user, {observe: 'response'})

Log the response to see how you can access the status.

Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
  • This works but how can I display the success login status code? For example: for success login server returns status code 200. – Storm Apr 18 '19 at 14:41
  • @Storm See the updated code if you want status irrespective of success or failure. – Ashish Ranjan Apr 18 '19 at 14:54