[英]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.