【问题标题】:Angular (8) Http.post() is giving SyntaxError: Unexpected token O in JSON at position 0 at JSON.parseAngular (8) Http.post() 给出 SyntaxError: Unexpected token O in JSON at JSON.parse 的位置 0
【发布时间】:2019-09-09 17:19:04
【问题描述】:

当我尝试将数据发布到我的服务器时,我收到了错误:

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();

【问题讨论】:

  • 您必须以这种方式responseType: 'text' 设置角度侧的响应类型,或者在服务器端将数据编码为json
  • @Ale8k 您也可以发布回复以找出您在回复中遇到错误的地方。
  • 是的,现在肯定会尝试 好的,所以我从服务器发回了 JSON 响应,它很高兴,我需要将哪个标头添加到我的帖子中以期待 Angular 中的状态?非常感谢@Sanoj_V

标签: json angular


【解决方案1】:

原来解决方案是 Angular 中 HTTP.post 上的响应类型选项,它没有与服务器对齐(类型为 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType)并且默认为 JSON。感谢所有回答的人!

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 2021-07-20
    • 2022-01-04
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2021-04-06
    相关资源
    最近更新 更多