Javascript 作为表单数据发送时,Post请求发送未定义
我已经安装了一个express应用程序 我已经确定了我的路线Javascript 作为表单数据发送时,Post请求发送未定义,javascript,node.js,http,express,Javascript,Node.js,Http,Express,我已经安装了一个express应用程序 我已经确定了我的路线 var ctrlLocations = require('../controllers/locations'); router.post('/locations', ctrlLocations.locationsCreate); module.exports = router; 这是locations.js文件 module.exports.locationsCreate = function(req, res){ cons
var ctrlLocations = require('../controllers/locations');
router.post('/locations', ctrlLocations.locationsCreate);
module.exports = router;
这是locations.js文件
module.exports.locationsCreate = function(req, res){
console.log(req.body.name);
}
当我使用Postman发送一个post请求时,如果我以x-www-form-urlencoded的形式发送身体,一切都会正常工作,我会看到我定义的“名称”
但是,当我发送post请求并将其正文作为表单数据时,会得到“未定义”
是的,我正在使用body解析器
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
如果要发送多部分/表单数据,则必须使用支持
内容类型的中间件<代码>正文分析器
不支持此操作。您应该使用类似于multer
的模块来支持多部分/表单数据。您可以添加表单代码吗?在app.use中设置extended:false
(bodyParser.urlencoded({extended:true}))代码>并重试。