我有一个快速应用程序和一个 POST 路由:
app.post('/test', function(req, res){
//res.send(req.body.title + req.body.body)
console.log(req.params);
console.log(req.body);
console.log(req.body.user);
console.log(req.body.feedback);
console.log("ok");
//return;
});
我尝试做 ~
$ curl -X POST "http://xxxx.herokuapp.com/test?user=hello"
并且我得到:TypeError: Cannot read property 'user' of undefined
at Router.<anonymous> (/app/app.js:40:22)
at done (/app/node_modules/express/lib/router/index.js:250:22)
at middleware (/app/node_modules/express/lib/router/index.js:244:9)
at param (/app/node_modules/express/lib/router/index.js:227:11)
at pass (/app/node_modules/express/lib/router/index.js:232:6)
at Router._dispatch (/app/node_modules/express/lib/router/index.js:255:4)
at Object.handle (/app/node_modules/express/lib/router/index.js:45:10)
at next (/app/node_modules/express/node_modules/connect/lib/http.js:198:15)
at Object.methodOverride [as handle] (/app/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:35:5)
at next (/app/node_modules/express/node_modules/connect/lib/http.js:198:15)~ $
那个 POST 不应该工作吗?
谢谢
最佳答案
不,您传递的参数不在帖子正文中,而是在查询字符串中。
根据 the docs ,要访问它,您必须执行以下操作:
req.query.user
关于javascript - Node.js:如何在 POST 中传递参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7053639/