2017-04-25 35 views
0

我不明白为什么令牌无法在请求中传递。服务器(java)不会收到令牌。标题中没有令牌请求(获取)Angular 2

服务(角2) ...

private authToken: string = localStorage.getItem('auth_token'); 
    private headers = new Headers({"Content-Type": "application/json"}); 
    // private options = new RequestOptions({headers: this.headers}); 

    constructor(private http: Http) { 
    this.headers.append("charset", "UTF-8"); 
    this.headers.append("X-auth_token", this.authToken) 
    } 

    getCommunes(territoryId: number): Observable<any>{ 
    return this.http 
     .get(`${urlBase}/forms/api/territoires/${territoryId}/communes`, this.headers) 
     .map(res => res.json()) 

    } 

... 如果我使用 “RequestOptions” 这是相同的。

浏览器控制台

console screenshot

网络(在请求的头部没有)

network screenshot

谢谢!

回答

1

我的猜测是你没有正确使用RequestOptions。试试这个:

private authToken: string = localStorage.getItem('auth_token'); 
private headers = new Headers({"Content-Type": "application/json"}); 

constructor(private http: Http) { 
    this.headers.append("charset", "UTF-8"); 
    this.headers.append("X-auth-token", this.authToken) 
} 

getCommunes(territoryId: number): Observable<any> { 
    let options: RequestOptions = new RequestOptions({ 
     headers: this.headers 
    }); 

    return this.http 
     .get(`${urlBase}/forms/api/territoires/${territoryId}/communes`, options) 
     .map(res => res.json()) 

} 

我也认为你应该使用的头名X-auth-token(以短划线,而不是下划线)

+0

什么是'_getInfoRequest'什么? @predreduc –

+0

@FredrikLundin这是我从我自己的项目中盲目复制的东西。 Woops。感谢您指出:) – PierreDuc

+0

感谢您的回应。我试过了,它不起作用。这可能是一个服务器问题... – yabe