【发布时间】: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}));
【问题讨论】:
-
你试过使用 angular $http 吗? docs.angularjs.org/api/ng/service/$http。像你一样组装数据对象并使用
$http.post('http://localhost:3000/operation', data)?
标签: javascript angularjs http express