Angular 4 And Node js

216 views Asked by At

In angular 4 at a client side, i have a post method when i click on it ->

 var json=JSON.stringify({id:"1",name:"par",title:"ssss"});
 var params='json'+json;
 this.http.post("http://localhost:3000/users/insertData",params, optio
 .subscribe(res => console.log(res.json()));}

after click on this at server side, I am using node express js, so the problem at server side I am getting data that type which is not acceptable for the database my SQL so data is getting ->

     { 'json{"id":"1","name":"par","title":"ssss"}' : ' ' }

but I want to get data is ->only JSON form

      {"id":"1","name":"par","title":"ssss"}

so please give me some solution ...

3

There are 3 answers

0
Adam Patterson On

This line: var params='json'+json;

You are concatenating a string with some data in JSON form.

I am not sure what you are trying to do with that, but that is why the data is not JSON when you receive it on the back end.

0
Sumit Agarwal On

By default Angular converts all your form submission data into json, even if you submit a model it is converted to json before sending it to the server. Unless you explicitly want to submit form data.

Secondly as pointed out by Adam, what are you trying to achieve using this piece of code var params='json'+json;

3
Raja Mohamed On

Try without stringify

var params={id:"1",name:"par",title:"ssss"}