When the form below is submitted against the following Node.js code, the data
event is not triggered at all; the code goes straight to req.on('end', callback);
function addEvent(req, res) {
"use strict";
var body = '';
req.setEncoding('utf8');
req.on('data', function(chunk) {
console.log('Got %d bytes of data', chunk.length);
body += chunk;
});
req.on('end', function() {
console.log('addEvent() request body: [%s]', body);
var postData = qs.parse(body);
dbInsertStmt.run(postData.name, postData.startDate, postData.startTime, postData.endTime, postData.anchor, postData.location, function() {
serveFromDisk('public/admin.html', res);
});
});
}
The form
<form action="../events" method="post" enctype="application/x-www-form-urlencoded">
<div class="form-group">
<label for="name">Nom</label>
<input type="text" class="form-control" id="name" placeholder="Nom de l'événement">
</div>
...
<button type="submit" class="btn btn-primary">Additionner événment</button>
</form>
What I see on the logs
Incoming request: POST /events
addEvent() request body: []
Any idea as to why the request body is empty?
The form's <input>
tags were missing the name
attribute. Without them, the browser does not send the fields at all.
The fix:
<input type="text" class="form-control" id="name" name="name" placeholder="Nom de l'événement">
I'm surprised as I copied the form HTML from Bootstrap's examples .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.