JavaScript / post给出了SyntaxError:意外的令牌o

我有下一个post要求:

POST /download HTTP/1.1 Host: localhost:5000 Content-Type: application/json 

和下一个请求有效载荷:

 {"blabla":"toto"} 

现在,在快速模块(node.js)中,我有下一个:

 app.post("/download", function(req, res){ var parseJson = JSON.parse(req.body); }); 

当我使用JSON.parse时,它给了我下一个错误:

 SyntaxError: Unexpected token o at Object.parse (native) at c:\NodeI\node\express.js:161:19 at callbacks (c:\NodeI\node\node_modules\express\lib\router\index.js:161:37) 

可能是什么原因? 我发了个JSON,为什么不parsing呢?

你的JSON在获得req.body的时候已经被parsing了

JSON.parse(req.body); 调用JavaScript对象的toString() ,获取string[object Object]并尝试将其parsing为JSON。

直接使用req.body而不是通过JSON.parse来运行它。