1

I'm using bootstrap to build a form, but when click the submit button, no POST data sent to my back-end server.

The code of Front is :

    <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>

And the backend is :

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);
});

But all I got is a '{}', where did I do wrong? The html part or the node part?

Matt
  • 74,352
  • 26
  • 153
  • 180
bxshi
  • 2,252
  • 5
  • 31
  • 50

1 Answers1

3

In your html action="/new_level" and all your inputs should have name attribute. Check here Node.js/Express form post req.body not working

Charlie Brumbaugh
  • 219
  • 1
  • 5
  • 15
vinayr
  • 11,026
  • 3
  • 46
  • 42