Node.js/Express 表单 post req.body 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7522034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js/Express form post req.body not working
提问by binarymax
I'm using express and having trouble getting form data from the bodyParser. No matter what I do it always comes up as an empty object. Here is my express generated app.js code (the only thing I added was the app.post route at the bottom):
我正在使用 express 并且无法从 bodyParser 获取表单数据。无论我做什么,它总是作为一个空对象出现。这是我快速生成的 app.js 代码(我添加的唯一内容是底部的 app.post 路由):
var express = require('express');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.sendfile('./public/index.html');
});
app.post('/', function(req, res){
console.log(req.body);
res.sendfile('./public/index.html');
});
app.listen(3010);
Here is my HTML form:
这是我的 HTML 表单:
<!doctype html>
<html>
<body>
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
<input type="text" id="mytext" />
<input type="submit" id="mysubmit" />
</form>
</body>
</html>
When I submit the form, req.body is an empty object {}
当我提交表单时,req.body 是一个空对象 {}
Its worth noting that this happens even if I remove the enctype attribute from the form tag
值得注意的是,即使我从表单标签中删除了 enctype 属性,也会发生这种情况
...Is there something I am missing/doing wrong?
...我有什么遗漏/做错了吗?
I am using node v0.4.11 and express v2.4.6
我正在使用节点 v0.4.11 并表达 v2.4.6
回答by Raynos
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
<input type="text" name="I_appear_in_req_body" id="mytext" />
<input type="submit" id="mysubmit" />
</form>
The body of a HTTP post is a key/value hash of all the form controls with a nameattribute, and the value is the value of the control.
HTTP 帖子的正文是所有具有name属性的表单控件的键/值哈希,值是控件的值。
You need to give names to all your inputs.
您需要为所有输入命名。
回答by Bhaveshkumar
It also due to content type. please see console.log(req) object.
这也是由于内容类型。请参阅 console.log(req) 对象。
'content-type': 'application/json; charset=UTF-8' // valid.
'content-type': 'application/JSON; charset=UTF-8' // invalid & req.body would empty object {}.
To check content type by console.log(req.is('json')) // return true/false
通过 console.log(req.is('json')) 检查内容类型 // 返回 true/false
I think 'charset=UTF-8' is negligible in above.
我认为上面的 'charset=UTF-8' 可以忽略不计。

