标题未设置

时间:2017-10-26 16:19:33

标签: angular http

我正在使用HttpClient发出请求,并且在每个请求中我都设置了标题但是当我看到chrome网络标签时,则没有设置这些标题。

代码

request(url: string, method: string, body?: Object) {
    // debugger;

    const fullUrl: string = this.baseUrl + '/' + url;
    var data: Object = new Object();

    const headers = new HttpHeaders();
    headers.append('Access-Control-Allow-Origin', '*');
    headers.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    headers.append('Access-Control-Allow-Headers', "X-Requested-With, Content-Type");
    headers.append('Authorization', 'Bearer '+this.auth.getToken());
    headers.append('Accept', 'application/json');

    data['headers'] = headers;


    if (body) {
        // debugger;
        data['body'] = body;
    }


    return this.http.request(method, fullUrl, data)
        .map((res: Response) => res)
        .catch((err: Response) => this.onRequestError(err));
}

网络标签图片

Chrome Network Tab

1 个答案:

答案 0 :(得分:1)

而不是data['headers'] = headers;

尝试

const req = new HttpRequest(method, fullUrl, data, { headers })
return this.http.request(req)
      .map((res: Response) => res)
      .catch((err: Response) => this.onRequestError(err));