Angular2 http.post,在头部发送请求

时间:2017-12-07 09:30:21

标签: javascript angular

let headers = new Headers();
headers.append('Content-Type', 'application/json');
let myReq = { FirstName: "AB", LastName: "CD"}

let options = new RequestOptions({ headers: headers });
const url = 'http://myurl.com';
return this.http.post(url, options)
        .map(res => res.json());  
  

如何在此帖子请求中注入json请求(myReq变量)作为此帖子请求的一部分。

1 个答案:

答案 0 :(得分:1)

尝试以下代码:

addBook() {

  let books = [
      { id: 1, name: 'Core Java' },
      { id: 2, name: 'Angular 2' },
      { id: 3, name: 'Hibernate' }
    ];

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });
    return this.http.post(this.url, book, options)
               .map(this.extractData)
               .catch(this.handleErrorObservable);
}