admin管理员组文章数量:1290346
I know that the question is findable on the forum but no answer works for me. I have an angular service that calls a nodeJS API like this:
Angular service
public createUser(pUser: User): Observable<User> {
var url = "http://localhost:5000/users";
var json = JSON.stringify(pUser)
return this.http.post<User>(url, pUser)
}
NodeJS API
router.post('/', (req, res) => {
console.log(req.body)
User.create({ email: req.body.email, password: req.body.password })
res.sendStatus(200);
});
The API is well called, the insertion into database works, but I have this message in the browser:
SyntaxError: Unexpected token O in JSON at position 0
The status of the request is 200 on return but the error is still present in the browser
I do not know if the problem es from the front end or the backend.
After doing some research, I try to parse my object before sending it. I try to stringify too.
Without success
Is this someone would have the solution? thank you very much
I know that the question is findable on the forum but no answer works for me. I have an angular service that calls a nodeJS API like this:
Angular service
public createUser(pUser: User): Observable<User> {
var url = "http://localhost:5000/users";
var json = JSON.stringify(pUser)
return this.http.post<User>(url, pUser)
}
NodeJS API
router.post('/', (req, res) => {
console.log(req.body)
User.create({ email: req.body.email, password: req.body.password })
res.sendStatus(200);
});
The API is well called, the insertion into database works, but I have this message in the browser:
SyntaxError: Unexpected token O in JSON at position 0
The status of the request is 200 on return but the error is still present in the browser
I do not know if the problem es from the front end or the backend.
After doing some research, I try to parse my object before sending it. I try to stringify too.
Without success
Is this someone would have the solution? thank you very much
Share Improve this question asked Nov 19, 2019 at 18:52 ChillAndCodeChillAndCode 3081 gold badge4 silver badges14 bronze badges 9- The API is probably parsing the JSON for you, so you're trying to parse the string "[Object ..." – Pointy Commented Nov 19, 2019 at 18:54
- I also have the error when I send the data directly without parse – ChillAndCode Commented Nov 19, 2019 at 18:56
- Well the key is to figure out what's in the actual HTTP data. Your browser has a "Network" developer tab that'll tell you that. – Pointy Commented Nov 19, 2019 at 18:59
- I looked, I can not find anything except status 200. The request payload is {email: "b", password: "b"} – ChillAndCode Commented Nov 19, 2019 at 19:02
- 3 Well "OK" is invalid JSON :) – Pointy Commented Nov 19, 2019 at 19:10
2 Answers
Reset to default 10This error occurs when you try to parse invalid JSON.
Due to the default response type for angular is JSON and the default response of code 200 is 'OK'. You have to adapt one of them.
You can change the response type to text
like this:
this.http.post<User>(url, pUser, {responseType: 'text'});
Or you return a JSON object:
res.status(200).send({ status: 'OK'});
It is good practise to send status 204 (No Content) if You don't send any content in response:
res.sendStatus(204);
Your Angular App should handle it and will not throw an error.
If You send status 200, it's good to add some json object, e.g. {status: "OK"}
in res.status(200).send({status: "OK"})
. Otherwise You will send the string 'OK' and will get "Unexpected token O ..." (O from 'OK').
From Express doc:
res.sendStatus(200) // equivalent to res.status(200).send('OK')
本文标签: javascriptUnexpected token O in JSON at position 0 when I query an APIStack Overflow
版权声明:本文标题:javascript - Unexpected token O in JSON at position 0 when I query an API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741497990a2381920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论