带头的angular2 http请求,不发送

时间:2016-09-08 09:57:24

标签: angular request http-headers

angular2的新手我尝试构建数据服务,我想添加到每个请求标头

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

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

答案 0 :(得分:1)

你需要用&#34; {}&#34;围绕你的this.headers ... ...像这样:

copy students (name, id, age, branch) from 'students.csv' with HEADER=TRUE;