nodejs的expresss中post的req.body总是undefined的原因

1)因为express将body-parser分离了出来,所以你需要手动添加进下面的内容即可

var path = require('path');
var bodyParser = require('body-parser');//用于req.body获取值的
app.use(bodyParser.json());
// 创建 application/x-www-form-urlencoded 编码解析
app.use(bodyParser.urlencoded({ extended: false }));

 

2)如果你传输的内容不是string类型时,你需要对上面的配置进行修改:

app.use(bodyParser.urlencoded({ extended: true}));

否则也总是会得到undefined的结果

转载于:https://www.cnblogs.com/wanghui-garcia/p/10040153.html

你可能感兴趣的:(nodejs的expresss中post的req.body总是undefined的原因)