簡體   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