我正在尝试在标头中发送x-api-key
标头,如下所示
service.ts
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class InsuranceServiceService {
public token: string;
constructor(private http: Http) {
const currentUser = JSON.parse(localStorage.getItem('currentUser'));
this.token = currentUser && currentUser.token;
}
createAuthorizationHeader(headers: Headers) {
headers.append('Content-Type', 'application/json');
headers.append('x-api-key', `xxxxxxxxxxxxxxxxxxxx`);
}
sendOTP(data: object): Observable<Object[]> {
const header = new Headers();
this.createAuthorizationHeader(header);
return this.http.post('http://my-omain.com/', data,
{
headers: header
})
.map((response: Response) => response.json());
}
}
但是,我收到以下错误:
预检响应中的Access-Control-Allow-Header不允许使用请求标头字段x-api-key。
如何解决此问题?是否有其他方式可以通过x-api-key
?
答案 0 :(得分:0)
这是服务器端问题,您必须配置服务器以允许所需的标头,搜索“CORS allow headers”