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