我和其他人正在经历这个错误,:SyntaxError: Unexpected token } in JSON at position 24 at JSON.parse (<anonymous>),同时遵循本教程关于JSON令牌(链接:https://www.youtube.com/watch?v=mbsmsi7l3r4&t=34s,大约分钟: 10:00)
此发帖请求期间出现错误:
Content-Type: application/json
{
"username": "Kyle",
},您可以在这里找到完整的代码: https://github.com/emanuelefavero/json-web-tokens
有三个主要文件需要注意:
错误似乎出现在server.js中,特别是在express-JWT方法中:
app.post('/login', (req, res) => {
// Authenticate User
// Create json web token
const username = req.body.username
const user = { name: username }
const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET)
console.log(accessToken)
res.json({ accessToken: accessToken })
})GET请求运行良好。
当我试图修复这个错误时,我仍然会回答任何问题,并在需要时提供项目细节。谢谢。
完全错误消息:
SyntaxError: Unexpected token } in JSON at position 24
at JSON.parse (<anonymous>)
at parse (/Users/arch0n/Desktop/JWT/json-web-token/node_modules/body-parser/lib/types/json.js:89:19)
at /Users/arch0n/Desktop/JWT/json-web-token/node_modules/body-parser/lib/read.js:128:18
at AsyncResource.runInAsyncScope (node:async_hooks:199:9)
at invokeCallback (/Users/arch0n/Desktop/JWT/json-web-token/node_modules/raw-body/index.js:231:16)
at done (/Users/arch0n/Desktop/JWT/json-web-token/node_modules/raw-body/index.js:220:7)
at IncomingMessage.onEnd (/Users/arch0n/Desktop/JWT/json-web-token/node_modules/raw-body/index.js:280:7)
at IncomingMessage.emit (node:events:402:35)
at endReadableNT (node:internal/streams/readable:1343:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21)发布于 2022-07-09 22:41:00
看来问题就在你提出的请求上。在请求中发送的JSON中有一个后缀逗号。
如果您刚开始使用JSON & JavaScript,只需记住JavaScript对象!== JSON。
在JS对象中,可以使用后缀逗号。但是,在JSON中,它会引起问题。
https://stackoverflow.com/questions/72925006
复制相似问题