Express:POST请求后未定义req.body [英] Express: Req.body is undefined after POST request

查看:172
本文介绍了Express:POST请求后未定义req.body的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在app.router之前使用express.bodyParser(),并且标题似乎是正确的,但是我在req.body上仍未定义:

I'm using the express.bodyParser() before the app.router, and the headers seem to be right, but I'm still getting undefined on req.body:

var app = express();
...

app.use(express.bodyParser());
...
app.use(app.router);

req.headers的输出是这样的:

The output of req.headers is this:

{ 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代码看起来不错.

You got Content-Length: 0, so the problem is on the client side. Your Express code looks OK.

这篇关于Express:POST请求后未定义req.body的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆