Node.js/Express form post req.body不工作
我正在使用express,从bodyParser获取表单数据时遇到问题。无论我做什么,它总是作为一个空的对象出现。下面是我的express生成的app.js代码(我唯一添加的是底部的app.post路径): 这是我的HTML表单:Node.js/Express form post req.body不工作,post,node.js,express,Post,Node.js,Express,我正在使用express,从bodyParser获取表单数据时遇到问题。无论我做什么,它总是作为一个空的对象出现。下面是我的express生成的app.js代码(我唯一添加的是底部的app.post路径): 这是我的HTML表单: <!doctype html> <html> <body> <form id="myform" action="/" method="post" enctype="application/x-www-form-urlenc
<!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>
提交表单时,req.body是一个空对象{}
值得注意的是,即使我从表单标记中删除了enctype属性,这种情况也会发生
…我有什么遗漏/做错了吗
我正在使用节点v0.4.11和express v2.4.6
<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>
HTTP post的主体是具有name
属性的所有表单控件的键/值散列,值是控件的值
您需要为所有输入命名。这也是由于内容类型的原因。请参阅console.log(req)对象 通过console.log(req.is('json'))检查内容类型//返回true/false 我认为‘charset=UTF-8’在上面可以忽略不计
<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>
'content-type': 'application/json; charset=UTF-8’ // valid.
'content-type': 'application/JSON; charset=UTF-8’ // invalid & req.body would empty object {}.