json - Angular (8) Http.post() 给出 SyntaxError: Unexpected token O in JSON at JSON.parse 的位置 0
问题描述
当我尝试将数据发布到我的服务器时,我收到了错误:
SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse...
我没有使用 JSON.parse() 所以我假设 Angulars Http.post() 自动执行此操作?
当我尝试 console.log(JSON.parse(data)) 时,我得到:
SyntaxError: Unexpected token o in JSON at position 1...
当我从 Postman 发送数据时 - 工作得很好,没有错误并且服务器接收到它。
当我在 fetch() api 请求中发送数据时 - 也可以正常工作,没有任何错误。
因此,这只发生在我尝试使用 angulars HTTP lib 并尝试发布时。下面是我发送的一些 JSON 数据的示例和我的 http 帖子的示例。
提前致谢!这一直让我发疯!
{"tutorialName":"cvbcvbxcvbxcvbbxcv","sections":[]}
this.http.post("http://127.0.0.1:5000/endpoint", JSON.stringify(this.tutorialForm.value)).subscribe();
解决方案
原来解决方案是 Angular 中 HTTP.post 上的响应类型选项,它与服务器不一致(https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType类型)并默认为 JSON。感谢所有回答的人!
推荐阅读
- python - 将文本文件转换为熊猫数据框的最佳方法是什么?
- wordpress - 显示页面顶部的简码中的 Wordpress 返回功能
- powershell - 使用 PowerShell 回复 Outlook 邮件
- python - python 3中连续随机数“*”
- html - 如何更改网站相对于屏幕的大小并保持标准分辨率?
- opencv - 手掌分割:cv2.erode 的替代品
- svelte - 如何在 svelte.js 中使用 svelte-router-spa 在多个页面上定义全局变量
- c - c(C90)中的分段错误,有什么问题?
- swift - SwiftUI 可选 @Binding 不会在第一次更改时更新其值
- python - 如何有不同宽度的代码单元和输出单元?