Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Express POST请求语法错误:JSON中位置1处出现意外标记o_Node.js_Json_Express - Fatal编程技术网

Node.js Express POST请求语法错误:JSON中位置1处出现意外标记o

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:

我试图使用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:\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));