如何将对象发送到http.post请求?

时间:2017-05-11 07:27:42

标签: angular ionic2

我的contact包含一个对象。我想发送到http.post方法。下面是我的代码,我应该在哪里放置contact对象作为参数。请给出与http相关的链接.post API,如果有的话。 代码:

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 个答案:

答案 0 :(得分:3)

Link to http post api根据要求

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

示例:

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