如何使用angular5为http请求添加自定义标头? 我尝试过这样的事情。
import {Headers} from 'angular2/http';
var headers = new Headers();
headers.append(headerName, value);
// HTTP POST using these headers
this.http.post(url, data, {
headers: headers
})
// do something with the response
答案 0 :(得分:0)
您可以在下面举例说明自定义标题:
import { Injectable } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
@Injectable()
export class AbcService {
constructor(private http: Http) { }
search = (query) => {
let headers = new Headers();
headers.append('Api-User-Agent', 'Example/1.0');
let apiUrl: string = 'https://en.wikipedia.org/w/api.phpgsrsearch='+query;
return this.http
.get(apiUrl, headers)
.map(response => response.json());
}
}