5

app.router の前に express.bodyParser() を使用しています。ヘッダーは正しいようですが、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);
    ...
});

私は何を間違っていますか?

4

2 に答える 2

1

私はこれに多くの問題を抱えていました..これは問題を解決するのに役立ちました.

var bodyParser = require('body-parser');

var app = Express();

// parse application/x-www-form-urlencoded
**app.use(bodyParser.json());**
app.use(bodyParser.urlencoded({ extended: false }));
于 2016-09-13T17:02:59.413 に答える