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的结果


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM