Angular 8 - Http post return error when is not error

Viewed 913

I try to make an http post request to a API. The problem is the follow: the API return a 200 status code, a success petition. But in my code works like in the http been an error.

My service code:

  getUsers(data) {
      const httpOptions = {
      headers: new HttpHeaders({
        "Accept": "application/json"
      })
      };

      let input = new FormData();
      input.append('firstName', data.firstName);

      return this.http.post('http://localhost/post.php', input, httpOptions);
    }

My component code:

callApi(data) {
    this.userService.getUsers(data)
    .subscribe(
      (data) => { // Success
        console.log(data)
      },
      (error) => {
        console.error("Error: " + JSON.stringify(error));
      }
    );
  }

The browser return: the follow

The http post result is the follow

What can be the problem?

1 Answers

You need to specify that the data to be returned is not JSON using the responseType.In your code, you can use a responseType string value of text, like this:

return this.http.post(
    'http://localhost/post.php',
    {responseType: 'text'})
Related