2017-07-27 37 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); 
    });