小编bin*_*max的帖子

Node.js/Express表单post req.body not working

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

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

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

......有什么我遗失/做错了吗?

我使用节点v0.4.11并表达v2.4.6

post node.js express

28
推荐指数
1
解决办法
4万
查看次数

标签 统计

express ×1

node.js ×1

post ×1