Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.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
Javascript NodeJS express can';无法接收POST json_Javascript_Jquery_Json_Node.js - Fatal编程技术网

Javascript NodeJS express can';无法接收POST json

Javascript 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

我正在尝试向我的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(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"})