Javascript NodeJS express can';无法接收POST json
我正在尝试向我的NodeJS路由发送一个JSONJavascript NodeJS express can';无法接收POST json,javascript,jquery,json,node.js,Javascript,Jquery,Json,Node.js,我正在尝试向我的NodeJS路由发送一个JSON curl -H "Content-Type: application/json" -d '{"name":"homer"}' http://localhost:3000/api 因此,在my server.js中: ... app.use(bodyParser.urlencoded({extended:true})); app.use(bodyParser.json({extended:true})); app.use(methodOverri
curl -H "Content-Type: application/json" -d '{"name":"homer"}' http://localhost:3000/api
因此,在my server.js中:
...
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json({extended:true}));
app.use(methodOverride('_method'));
...
然后,在我的路线上:
router.post('/api', function (req, res){
console.log(req.body);
});
因此,输出显示undefined
我做错什么了吗?我使用的是Express v4。JSON stringify从对象生成字符串。 使用 或
Express 4中的中间件和路由按添加到应用程序的顺序执行。因此,您需要确保在使用
bodyParser
中间件之后才使用路由。相同的未定义的问题。我想这可能是由于主体解析器
的原因,因为我正在尝试使用curl
并显示相同的错误尝试删除ajax postsame错误中的contentType。我肯定是后端的东西。我使用这个curl来测试:curl-H“Content-Type:application/json”-d'{“name”:“homer”}”http://localhost:3000/api
和不工作通过使用路由器
在正文解析器
之后解决。非常感谢。您确定在读取请求数据的bodyParser
中间件之前没有中间件吗?@mscdex您找到了。在bodyparser
之后使用router
解决。谢谢
JSON.parse({"name":"homer"})
$.param({"name":"homer"})