简体   繁体   English

Angular(8)Http.post()提供SyntaxError:JSON.parse位置0处JSON中的意外令牌O

[英]Angular (8) Http.post() is giving SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse

When I try post data to my server, I'm getting the error: 当我尝试将数据发布到服务器时,出现错误:

SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse...

I am not using JSON.parse() so I assume Angulars Http.post() automatically does this? 我没有使用JSON.parse(),所以我假设Angulars Http.post()会自动执行此操作吗?

When I try console.log(JSON.parse(data)), I'm getting: 当我尝试console.log(JSON.parse(data))时,我得到:

SyntaxError: Unexpected token o in JSON at position 1...

When I send the data from Postman - works perfectly fine, no errors and the server receives it. 当我从Postman发送数据时-可以正常工作,没有错误,服务器接收到它。

When I send the data in a fetch() api request - also works perfectly fine, no errors whatsoever. 当我在fetch()api请求中发送数据时-也可以正常工作,没有任何错误。

So, this only occurs when I try use angulars HTTP lib and try to post. 因此,仅当我尝试使用angulars HTTP lib并尝试发布时,才会发生这种情况。 Below is an example of some JSON data I send and an example of my http post. 以下是我发送的一些JSON数据示例和http帖子的示例。

Thanks in advance! 提前致谢! This has been driving me crazy! 这让我发疯了!

{"tutorialName":"cvbcvbxcvbxcvbbxcv","sections":[]}
this.http.post("http://127.0.0.1:5000/endpoint", JSON.stringify(this.tutorialForm.value)).subscribe();

Turns out the solution was the response type option on HTTP.post in Angular, it didn't align up with the server ( https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType for types) and defaults to JSON. 原来该解决方案是Angular中HTTP.post上的响应类型选项,它与服务器不符( https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType类型),默认为JSON。 Thank you to all who answered! 谢谢所有回答的人!

暂无
暂无

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

相关问题 Angular 6: SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse with valid JSON - Angular 6: SyntaxError: Unexpected token O in JSON at position 0 at JSON.parse with valid JSON JSON中JSON.parse位置0处的意外令牌o - Unexpected token o in JSON at position 0 at JSON.parse using angular JSON.parse 错误(未捕获的 SyntaxError:JSON 中的意外标记 o 在 position 1) - JSON.parse error (Uncaught SyntaxError: Unexpected token o in JSON at position 1) 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse NodeJs JSON.parse - SyntaxError:JSON 中的意外标记 u 在位置 0 - NodeJs JSON.parse - SyntaxError: Unexpected token u in JSON at position 0 JSON.parse SyntaxError:意外令牌? 在 JSON 中的位置 124 - JSON.parse SyntaxError: Unexpected token ? in JSON at position 124 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) JSON.parse() 导致错误:`SyntaxError: Unexpected token in JSON at position 0` - JSON.parse() causes error: `SyntaxError: Unexpected token in JSON at position 0` SyntaxError:JSON中的意外令牌&lt;位于JSON.parse(位置1) <anonymous> ) - SyntaxError: Unexpected token < in JSON at position 1 at JSON.parse (<anonymous>) JSON.parse(): SyntaxError: Unexpected token in JSON at position 0 - JSON.parse(): SyntaxError: Unexpected token � in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM