我正在尝试添加一个请求的标头,以便从请求特定授权名称的API接收数据,该值具有我的关键值。我花了好几个小时。 如果我使用谷歌浏览器的插件手动添加名称和值,该请求将起作用。 我有一个服务,我用来运行get请求从API中提取,但API请求请求标头。有什么想法吗?
从@ angular / core'导入{Injectable}; 从' @ angular / common / http'中导入{HttpClient,HttpErrorResponse,HttpHeaders}; 从' rxjs'导入{Observable,BehaviorSubject,combineLatest,Subject}; 从' rxjs / operators';
导入{map,take,merge,switchMap}@Injectable({ 提供:' root' }) 导出类CjaffiliateService {
constructor(public http: HttpClient) { }
headerName = 'authorization';
// tslint:disable-next-line:max-line-length
key = 'key'
cjURL = 'wwww.url.used';
getAdvertisers() {
let headers: HttpHeaders = new HttpHeaders();
headers = headers.append('Content-Type', 'application/json');
headers = headers.append(this.headerName, this.key);
const options = {headers};
console.log(headers);
const getRequest = this.http.get(this.cjURL, options);
const subscription = getRequest.subscribe((x) => console.log(x));
console.log(getRequest);
return getRequest;
}
}