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

新手上路,请多包涵

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

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

CORS 策略已阻止从来源“http://acp56df5alc.execute-api.us-east-1.amazonaws.com/ams/getmember”访问 XMLHttpRequest“ http://localhost:4200 ”:对预检的 响应 请求未通过访问控制检查:请求的资源上不存在“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;
            }));
    }

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

原文由 Isanka Thalagala 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 427
1 个回答

我有相同的 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}
    )

原文由 ovicko 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题