这是我的有角度的职位要求:

    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所指出的,传递正确的标头。

阅读更多here

09-30 16:28
查看更多