Sunday, 4 June 2017

Angular 2 http post is sent as empty object or on the key side

I'm trying to send data from angular 2 to node js on the server side my angular code is

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

  }

and on the server side

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

when I console.log it on the server side I get an empty object {} I tried using JSON.stringify on the data on the client side but no help

N.B. I'm using cors dependency on the server side



via Mahmoud Youssef

No comments:

Post a Comment