【发布时间】:2022-01-18 13:14:54
【问题描述】:
当我使用 Angular 向我的 API 网关发送请求时,出现此错误:
*CORS 策略已阻止从源“http://localhost:4200”访问“https://example.com”处的 XMLHttpRequest:请求的资源上不存在“Access-Control-Allow-Origin”标头。 *
如果我添加 'Access-Control-Allow-Origin':'*' 我有这个:
*从源“http://localhost:4200”访问“https://example.com”上的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问控制-请求的资源上存在 Allow-Origin' 标头。*
但同样的请求可以完美地与 CURL、Web 浏览器和另一个移动应用程序 (Flutter) 配合使用。
我尝试在 API Gateway 中启用(使用 Access-Control-Allow-Origin:'*')并禁用 CORS,但问题仍然存在。
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class mainService {
constructor(private http: HttpClient) {}
getAll() {
return this.http.get<any>("https://example.com",
{
headers: HttpHeaders({'Access-Control-Allow-Origin':'*'})
});
}
}
【问题讨论】:
标签: angular cors aws-api-gateway