1

My contact contains an object.I want to send to the http.post method.Below is my code, where should I put contact object as a parameter.Please give links also related to http.post API if any. Code:

createContacts(contact) {
    console.log('the contact inside subscribe function',contact);
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:8080/contacts',{headers : headers}).subscribe(res => {
      console.log('inside postmehtod of sub.function', res.json());//only objects

    });

  }

package.json

{
  "name": "contactlist",
  "version": "0.1.0",
  "description": "A sample Node.js app using Express 4",
  "engines": {
    "node": "5.9.1"
  },
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "devDependencies": {
    "mongoose": "^4.6.2",
    "body-parser": "^1.15.2",
    "cors": "^2.8.0",
    "del": "2.2.0",
    "express": "^4.14.0",
    "http": "0.0.0",
    "method-override": "^2.3.6",
    "morgan": "^1.7.0",
    "superlogin": "^0.6.1"
  }
}

1 Answer 1

4

Link to http post api as requested

Typescript post API: post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>

Example:

createContacts(contact) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    this.http.post('http://localhost:8080/contacts', contact, {headers : headers})
      .subscribe(res => {
         console.log('inside postmehtod of sub.function', res.json());//only objects
      })
  }
Sign up to request clarification or add additional context in comments.

4 Comments

I got the required result.Can you explain me why this is not working. this.http.post('http://localhost:8080/contacts',JSON.stringify(contact),...) here I have added json.stringify why the code is not working with this line?
Can you describe how the code is not working? It should work. I can not reproduce this, although I am not using ionic. Regardless of whether I use bodyParser here in the back end and/or JSON.stringify on the front end, I always get the same data from the post method on my node.js backend
Strange. Which backend are you using? Which angular version are you using? Please do a git clone https://github.com/jsdevtom/so_example.git && npm i && npm run debug, then open the chrome link in the console which should look something like chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/9b6c1f94-ddab-45f2-a551-0d9ee78c0f98

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.