繁体   English   中英

带头的angular2 http请求,不发送

[英]angular2 http request with headers, do not send

我对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());
    }
}

如何正确执行此操作,或者为什么此方法不起作用?

您需要在“ this.header”周围加上“ {}” ...,如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM