首页 > 解决方案 > Angular 7:对预检请求的响应未通过访问控制检查:请求中不存在“Access-Control-Allow-Origin”标头

问题描述

我必须用 CURD 操作实现一个角度应用程序。API 已经托管在 AWS 中,这与 Postman 配合得很好。

但是我的角度应用程序越来越

从源“ http://localhost:4200 ”访问位于“ https://acp56df5alc.execute-api.us-east-1.amazonaws.com/ams/getmember ”的 XMLHttpRequest已被 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;
            }));
    }

我已经努力解决这个问题。但需要一些帮助

标签: javascriptangularamazon-web-servicesangular7

解决方案


我有同样的cors问题,并尝试了所有建议的设置方式,Access-Control-Allow-Origin *但没有成功。
后来发现两个问题:

  1. data format我通过请求发送的POST格式不正确。
  2. 服务器无法处理从发布请求接收到的空参数。

原始请求:

return this.http.post(API_URL + 'customer/login',
  {email: email, password: password},{ headers: headers}
)

在我使用包裹发布数据后工作JSON.stringify()

return this.http.post(API_URL + 'customer/login',
      JSON.stringify({email: email, password: password}),{ headers: headers}
    )

推荐阅读