2

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?

2
  • can you post what you see inside JSON.stringify(error); Commented Oct 27, 2019 at 12:30
  • @Sajeetharan i update the post. The screenshot is here: i.sstatic.net/M3UPH.png Commented Oct 27, 2019 at 12:38

1 Answer 1

1

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'})
Sign up to request clarification or add additional context in comments.

2 Comments

I try that but it returns a CORS error. I need to check that
when calling from another domain you might need to enble cors on the backend beacuse that can actually throw an error or any other backenc error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.