简体   繁体   English

Angular 9 HttpErrorResponse 'JSON.Parse error' 虽然响应正常

[英]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)" "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) 在 Object.onInvokeTask (https://localhost:5001/vendor.js:72879:33) 在 ZoneDelegate.invokeTask (https://localhost:5001/polyfills.js:411:40) ) 在 Zone.runTask (https://localhost:5001/polyfills.js:180:51) 在 ZoneTask.invokeTask [as invoke] (https://localhost:5001/polyfills.js:493:38) 在 invokeTask ( https://localhost:5001/polyfills.js:1634:18) 在 XMLHttpRequest.globalZoneAwareCallback (https://localhost:5001/polyfills.js:1671:25)"

The response is plain string value and has status 200.响应是纯字符串值,状态为 200。

在此处输入图像描述

在此处输入图像描述

What have I missed?我错过了什么?

Update the deleteUser method in your dataService with the correct responseType (JSON is the default).使用正确的 responseType 更新 dataService 中的 deleteUser 方法(默认为 JSON)。 Also, I'm assuming that's where you use the httpClient.另外,我假设那是您使用 httpClient 的地方。

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

You are returning text, not JSON.您正在返回文本,而不是 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM