json - Angular 9 HttpErrorResponse 'JSON.Parse error' 响应正常

标签 json angular typescript angular-httpclient

为什么会抛出错误?

 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)"

响应是纯字符串值,状态为 200。

enter image description here

enter image description here

我错过了什么?

最佳答案

使用正确的响应类型(默认为 JSON)更新 dataService 中的 deleteUser 方法。另外,我假设这就是您使用 httpClient 的地方。

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

您返回的是文本,而不是 JSON。

关于json - Angular 9 HttpErrorResponse 'JSON.Parse error' 响应正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63230566/

相关文章:

php - 从特定 Id JSON 获取特定对象

javascript - json声明后未定义的js数组

angular - 如何将 Angular Material 表数据刷新回其在页面内的初始状态?

typescript 和 Jest : mocking throws typerror because its using the wrong overload

带接口(interface)的 Angular 6 服务

reactjs - 如何使用reactjs的链接代理

java - JSON 中 map 的带点路径 (Java)

php - 在android中创建动态表单

angular - 如何更改 Angular Validators 中 required 的验证消息

javascript - 从 RXJS observable 获取中间数据的最佳方式