2017-07-27 18 views
0

私のAngular投稿要求の本文は私のサーバーでは空です。私のクライアントアプリケーションからのPOSTリクエストは、次のとおりです。Angular投稿要求の本文が空です

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"); 
}); 

私はapp.post(...) {}インサイド

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

を持っているあなたは、データがこのように使用可能になるのを待つ必要があります。

var body = ''; 
    req.on('data',function(data) { body += data; }); 
    req.on('end', function(data) { 
     req.body = JSON.parse(body); 
     conosle.log(request.body); 
    }); 
関連する問題