anatol
anatol

Reputation: 1762

Angular 9 HttpErrorResponse 'JSON.Parse error' while response is OK

Why this throws an error?

 deleteUser(userId: string) {
    this.dataService.deleteUser(userId).subscribe((response: string) => {
      console.log(response);
    });
  }

"SyntaxError: Unexpected token f in JSON at position 1 at JSON.parse () at XMLHttpRequest.onLoad (https://localhost:5001/vendor.js:34968:51) at ZoneDelegate.invokeTask (https://localhost:5001/polyfills.js:412:35) at Object.onInvokeTask (https://localhost:5001/vendor.js:72879:33) at ZoneDelegate.invokeTask (https://localhost:5001/polyfills.js:411:40) at Zone.runTask (https://localhost:5001/polyfills.js:180:51) at ZoneTask.invokeTask [as invoke] (https://localhost:5001/polyfills.js:493:38) at invokeTask (https://localhost:5001/polyfills.js:1634:18) at XMLHttpRequest.globalZoneAwareCallback (https://localhost:5001/polyfills.js:1671:25)"

The response is plain string value and has status 200.

enter image description here

enter image description here

What have I missed?

Upvotes: 2

Views: 5699

Answers (1)

user2263572
user2263572

Reputation: 5606

Update the deleteUser method in your dataService with the correct responseType (JSON is the default). Also, I'm assuming that's where you use the httpClient.

httpClient.get('your_endpoint', { responseType: 'text' }).subscribe(result => {
    console.log(result);
});

You are returning text, not JSON.

Upvotes: 6

Related Questions