Node.js Express POST请求语法错误:JSON中位置1处出现意外标记o
我试图使用node和express获取POST请求的JSON请求体。但是,我从express中得到以下错误:Node.js Express POST请求语法错误:JSON中位置1处出现意外标记o,node.js,json,express,Node.js,Json,Express,我试图使用node和express获取POST请求的JSON请求体。但是,我从express中得到以下错误: SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) at parse (C:\Users\Bradley\Desktop\test\node_modules\body-parser\lib\types\json.js:89:19) at C:
SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at parse (C:\Users\Bradley\Desktop\test\node_modules\body-parser\lib\types\json.js:89:19)
at C:\Users\Bradley\Desktop\test\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (C:\Users\Bradley\Desktop\test\node_modules\raw-body\index.js:224:16)
at done (C:\Users\Bradley\Desktop\test\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (C:\Users\Bradley\Desktop\test\node_modules\raw-body\index.js:273:7)
at IncomingMessage.emit (events.js:323:22)
at endReadableNT (_stream_readable.js:1204:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
为了触发错误,我正在提交请求:
fetch('http://localhost:5680/api', {
method: 'POST',
headers: {'Content-type': 'application/json; charset=UTF-8'},
body: {"key": "value"}
}).then(response => console.log(response));
body解析器似乎在双重解析JSON,但我不明白为什么。问题在于请求。正文应为字符串,因此完整请求为:
fetch('http://localhost:5680/api', {
method: 'POST',
headers: {'Content-type': 'application/json; charset=UTF-8'},
body: '{"key": "value"}'
}).then(response => console.log(response));
fetch('http://localhost:5680/api', {
method: 'POST',
headers: {'Content-type': 'application/json; charset=UTF-8'},
body: '{"key": "value"}'
}).then(response => console.log(response));