我正在尝试从HTML表单向JavaScript服务器发送数据。
我使用Postman工具测试了POST请求,在服务器端,一切正常。
问题是,从HTML表单中我得到一个空的身体。
这是HTML表单
<form name="newsletter" enctype="application/json" action="/add-to-newsletter" method="post" >
<div class="row uniform">
<div class="6u 12u$(medium)">
<input type="email" name="email" placeholder="Email" />
</div>
<div class="6u$ 12u$(small)">
<ul class="actions">
<li><input type="submit" value="INVIA" class="special" /></li>
</ul>
</div>
</div>
<p style="margin-top:2.1em; font-size:1em;">Riceverai solo cose utili e divertenti</p>
</form>
这是Javascript服务器
var express = require('express');
var db = require('./db.js');
var middleware = require('./middleware.js')(db);
var _ = require('underscore');
var bodyParser = require('body-parser');
var app = express();
var PORT = process.env.PORT || 3000;
app.use(bodyParser.json());
//POST /addNewsletter
app.post('/add-to-newsletter', function (req, res) {
/*debug*/ console.log("This is the body you are trying to post in add-to-newsletter");
/*debug*/ console.log(req.body);
var body = _.pick(req.body, 'email');
body.email = body.email.trim();
body.newsletter = true;
db.newsletterUser.create(body).then(function (user) {
//mailing.sendLoginEmail(user.toJSON().email); /*deactivate not to send login mail*/
res.json(user.toPublicJSON());
}).catch(function (e) {
/*DEBUG*/ console.log("Error in addNewsletter");
/*DEBUG*/ console.log(e);
res.status(400).json(e);
});
});
// VARIOUS
app.use(express.static(__dirname + '/public'));
//LISTEN - REMOVE {force: true} NOT TO ERASE DB
db.sequelize.sync({force: true}).then(function () {
app.listen(PORT, function (){
console.log('Express listening on port ' + PORT);
});
});
我一直在尝试不同的方法,例如将enctype更改为application/x-www-form-urlencoded
并使用教程中的外部表单,但我总是得到相同的结果。