2017-07-27 61 views
0

所以我的Angular post请求的主体在我的服务器上是空的。从我的客户端应用程序的POST请求:我的Angular post请求的主体是空的

var data = { 
     Stime: '+this.Stime+', 
     Etime: '+this.Etime+', 
     eAMPM: '+this.eAMPM+', 
     sAMPM: '+this.sAMPM+', 
     id: '+this.id+', 
     activity: '+this.activity+', 
     auto_insert: '+this.auto_insert+', 
     yearmonthday: '+this.yearmonthday+', 
     color1: '+this.c+' 
    } 
    this.$http({ 
     method: 'POST', 
     url: 'http://localhost:3000/operation', 
     data: JSON.stringify(data) 
    }) 

之前,我尝试使用这个职位要求我刚做了一个很长的查询字符串,这传递的数据就好了。但我现在正在清理我的代码,这种方式不起作用。 在我的服务器我有:

app.post('/operation', function(req,res){ 
    console.log(req.body); //prints {} 
    res.send("inserted"); 
}); 

而且在服务器端我有

var express = require('express'), 
app = express(), 
bodyParser = require('body-parser'), 
cookieParser = require('cookie-parser'), 
multer = require('multer'), 
jsonParser = bodyParser.json() 

app.use(jsonParser) 
app.use(bodyParser.urlencoded({ 
    extended: true 
})) 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({extended: true})); 
+0

有你尝试使用角$ http? https://docs.angularjs.org/api/ng/service/$http。像你一样组装数据对象 并使用'$ http.post('http:// localhost:3000/operation',data)'? –

回答

0

里面app.post(...) {}你需要等待数据变得可用这样的:

var body = ''; 
    req.on('data',function(data) { body += data; }); 
    req.on('end', function(data) { 
     req.body = JSON.parse(body); 
     conosle.log(request.body); 
    });