2017-12-07 2 views
-1
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()); 

Wie kann ich json Anfrage (myReq Variable) innerhalb Header als Teil dieser Post-Anforderung zu injizieren.Angular2 http.post, das Senden Anfrage innerhalb Header

Antwort

1

versuchen, wie unten Code:

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

ich hoffe, dass dies für Sie arbeiten, wenn es richtig als Lösung bitte arbeiten. –