简体   繁体   English

我的Angular发布请求的正文为空

[英]The body of my Angular post request is empty

So the body of my Angular post request is empty on my server. 因此,我的Angular发布请求的主体在服务器上为空。 The post request from my client application is: 我的客户应用程序的发帖请求是:

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

before I tried using this post request I just had a very long query string and this passed the data just fine. 在尝试使用此发帖请求之前,我只有一个非常长的查询字符串,并且这很好地传递了数据。 But I am cleaning up my code now and this way isnt working. 但是我现在正在清理代码,这种方式行不通。 On my server I have: 在我的服务器上,我有:

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

Also on the server side I have 另外在服务器端

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

Inside app.post(...) {} you need to wait for data to become available like this: 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);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM