【发布时间】:2017-11-05 15:08:48
【问题描述】:
我正在尝试将数据从 angular 2 发送到服务器端的节点 js 我的角码是
addCrib(data:any) {
let bodyString = data;
let headers = new Headers({ 'Content-Type': 'application/json' });
return this.http.post('http://localhost:8000/addcribs',data , {headers:headers}).map(res => res.json()).subscribe(
data => console.log(data)
);
}
在服务器端
addcribs: function(req,res){
var x = req.body;
console.log(x); // {} with application/json and { '{data....}':''} with application/x-www-form-urlencoded
let crib = new Cribs({id:0, type:req.body.type,price:req.body.price,address:req.body.address,description:req.body.description,bedrooms:req.body.bedrooms,bathroom:req.body.bathroom,area: req.body.area,image:req.body.image});
crib.save(function(err,crib){
if(!err){
res.json({success:true});
}
})
}
当我在服务器端 console.log 时,我得到一个空对象 {},当我使用 contentType: application/json 时,当我使用 contentType: application/x-www-form-urlencoded 我得到所有数据对象的关键面,例如。 {'{type:house}':''} 我尝试在客户端的数据上使用 JSON.stringify 但没有帮助
注意我在服务器端使用 cors 依赖
【问题讨论】:
标签: javascript node.js angular http post