我在 app.router 之前使用 express.bodyParser(), header 似乎是正确的,但我在 req.body 上仍然未定义:
var app = express();
...
app.use(express.bodyParser());
...
app.use(app.router);
req.headers 的输出是这样的:
{ host: '127.0.0.1:3000',
connection: 'keep-alive',
'content-length': '0',
'cache-control': 'max-age=0',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
origin: 'http://127.0.0.1:3000',
'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36',
'content-type': 'application/x-www-form-urlencoded',
referer: 'http://127.0.0.1:3000/register',
'accept-encoding': 'gzip,deflate,sdch',
'accept-language': 'es-ES,es;q=0.8' }
帖子声明如下:
app.post('/register/do', function(req, res) {
...
console.log(req.headers);
console.log(req.body);
...
});
我做错了什么?
最佳答案
您得到了 Content-Length: 0
,所以问题出在客户端。您的 Express 代码看起来没问题。
关于node.js - Express:POST 请求后 Req.body 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920295/