我需要实现一个具有CURD操作的Angular应用程序。API已经托管在AWS上,使用Postman可以正常工作。
但是我的Angular应用程序出现了以下问题:
访问来自源 'http://localhost:4200' 的XMLHttpRequest到'https://acp56df5alc.execute-api.us-east-1.amazonaws.com/ams/getmember'已被CORS策略阻止:响应预检请求未通过访问控制检查:所请求的资源上不存在“Access-Control-Allow-Origin”头。
以下是我的代码:
http_GET(actionUrl: string): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'key': 'x-api-key',
'value': 'NNctr6Tjrw9794gFXf3fi6zWBZ78j6Gv3UCb3y0x',
})
};
return this.http.get<any>(this.baseUrl + actionUrl, httpOptions).pipe(
(response => {
return response;
}));
}
我已经努力尝试解决这个问题了,但还需要一些帮助。