无法从request.body中提取发布的数据
这是我的angular度发帖要求:
var webCall = $http({ method: 'POST', url: '/validation', async : true, headers: { 'Content-Type': 'text/html' }, data: {email: "abc@gmail.com"}}); webCall.then(successHandler, errorHandler);
现在在我的nodejs服务器中,以下代码提取发布数据:
app.post('/validation',function(req,res){ req.on('data',function(data){ console.log(data.toString()); });
但是安慰请求体为:
app.post('/validation',function(req,res){ console.log(req.body); }
控制台空的对象。
一个可能的原因可能是你没有添加(或在问题中显示) body-parser
中间件。
var bodyParser = require('body-parser'); // for parsing application/json app.use(bodyParser.json()); // for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // for parsing multipart/form-data app.use(multer());
在您希望接收的数据的routes
之前添加相关的中间件。 就你而言,看起来你只需要第一个。 另外,正如@ themyth92所指出的那样,传递正确的标题。
在这里阅读更多