无法使用bootstrap和node获取POST数据

时间:2013-01-16 04:23:57

标签: javascript html node.js

我正在使用bootstrap来构建表单,但是当单击“提交”按钮时,没有POST数据发送到我的后端服务器。

Front的代码是:

    <form class="form-horizontal" id="level_form" method="post" action="" data-confirm="Ready to submit?">
            <div class="control-group">
                <label class="control-label" for="startDate"></label>
                <div class="controls">
                    <input type="date" id="startDate">
                    <span class="label label-important hidden" id="startDateHelper"></span>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="stopDate"></label>
                <div class="controls">
                    <input type="date" id="stopDate">
                    <span class="label label-important hidden" id="stopDateHelper"></span>
                </div>
            </div>

            <div class="form-actions">
                <button type="submit" class="btn btn-primary">submit</button>
                <button type="button" class="btn">preview</button>
            </div>
    </form>

后端是:

app.use(express.bodyParser());


app.engine('html', cons.mustache);
app.set('view engine', 'html');
app.set('views', __dirname+'/views');
app.use(express.static(__dirname + '/public'));


app.use(express.basicAuth(function(user, pass){
    return user == 'admin' && pass == 'solid'
}));

app.use(app.router);



app.get('/', function(req, res){
    res.render('index', {});
});

app.get('/new_level', function(req, res){
    res.render('level_editor', {action_url:'/new_level'});
});
app.post('/new_level', function(req, res){
    console.log(req.body);
    res.send(req.body);
});

但我得到的只是'{}',我在哪里做错了? html部分还是节点部分?

1 个答案:

答案 0 :(得分:3)

在您的html action="/new_level"中,您的所有输入都应具有name属性。点击Node.js/Express form post req.body not working