我正在使用中间件body-parser
对表单值进行编码以获取 req.body 对象。
但当我调试代码时,发现 req.body 未定义。这是我的代码
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
监听帖子请求
app.post('/newCategory', function (req,res) {
//express attached the form encoded values into body
var categoryName = req.body.categoryName;
});
网页表单
<form action="/newCategory" role="form" method="post" class="form-inline">
<input type="text" name="categoryName" placeholder="Category name" class="form-control" />
<input type="submit" value="New Category" class="btn btn-primary" />
</form>
刚刚遇到了同样的问题。看起来我通过将代码移动到 urlencoded 行之后的映射路线来解决了我的问题。我现在在帖子中看到 req.body 。
app.use(bodyParser.urlencoded({ extended: true }));
// Map routes
var controllers = require("./controllers");
controllers.init(app);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)