Angular 9 HttpErrorResponse 'JSON.Parse 错误',当响应正常时
为什么这会报错?
deleteUser(userId: string) {
this.dataService.deleteUser(userId).subscribe((response: string) => {
console.log(response);
});
}
"SyntaxError: 在位置 1 的 JSON 中出现意外的标记 f 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 [作为 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。
我错过了什么?
答案#1
在你的 dataService 中更新 deleteUser 方法,使用正确的 responseType(默认是 JSON)。另外,我假设你就是在那里使用 httpClient。
httpClient.get('your_endpoint', { responseType: 'text' }).subscribe(result => {
console.log(result);
});
你返回的是文本,而不是 JSON。

