2016-09-08 27 views
0

很新的angular2我尝试建立一个数据服务,我想添加到每个请求头angular2 HTTP与头的要求,不发

这里是我的尝试,但标题不会被发送

import { Injectable } from '@angular/core'; 
import { Http, Response, Headers } from '@angular/http'; 
import 'rxjs/add/operator/map' 
import { Observable } from 'rxjs/Observable'; 
import { Configuration } from '../app.constants'; 
import { AuthenticationService } from '../services/'; 

@Injectable() 
export class DataService { 

    private actionUrl: string; 
    private headers: Headers; 
    private token; 


    constructor(private _http: Http, 
       private _configuration: Configuration 

       //private authenticationService: AuthenticationService 

    ) { 

     var currentUser = JSON.parse(localStorage.getItem('currentUser')); 

     if(currentUser) 
      this.token = currentUser.token; 

     this.actionUrl = _configuration.ServerWithApiUrl + 'patients'; 

     this.headers = new Headers(); 

     this.headers.append('Content-Type', 'application/x-www-form-urlencoded'); 
     this.headers.append('Accept', 'application/json'); 
     this.headers.append('Authorization', 'Bearer ' + this.token); 
    } 

    public GetAll =(): Observable<any> => { 
     return this._http.get(this.actionUrl, this.headers).map((response: Response) => <any>response.json()); 
    } 
} 

如何正确地做到这一点或为什么这不起作用?

回答

1

你需要与你的周围this.headers “{}” ......像这样:

return this.http.get(this.constants.accountLocalUrl, { headers: headers })