我正在使用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));
}
网络标签图片
答案 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));