微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Node.js/Express表单post req.body不工作

我正在使用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('/',res){
    console.log(req.body);
    res.sendfile('./public/index.html');
});

app.listen(3010);

这是我的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>

当我提交表单时,req.body是一个空的对象{}

值得注意的是,即使我从表单标签删除了enctype属性,也会发生这种情况

有没有什么我错过/做错了?

我正在使用节点v0.4.11和表达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文件的正文是具有name属性的所有表单控件的键/值哈希值,该值是控件的值。

你需要给所有输入的名字。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐