【问题标题】:The body of my Angular post request is empty我的 Angular 发布请求的正文为空
【发布时间】:2017-07-27 20:07:12
【问题描述】:

所以我的 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");
});

在服务器端我也有

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

【问题讨论】:

标签: javascript angularjs http express


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-03-12
    • 2019-06-30
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多