简体   繁体   English

AWS Api Gateway 403错误-从所有来源启用CORS

[英]AWS Api Gateway 403 error - CORS enabled from all origins

I have created a lambda function which is to be accessed via an API Gateway. 我创建了一个lambda函数,可以通过API网关进行访问。 However whenever a post request is made I get the following error 但是,每当发出发布请求时,都会出现以下错误

在此处输入图片说明

When I navigate to was console and look at API Gateway I have 当我导航到控制台并查看API Gateway时,

在此处输入图片说明

Finally my code (Angular2) does add the appropriate headers to the request by: 最后,我的代码(Angular2)确实通过以下方式将适当的标头添加到请求中:

var headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');

this.http.post('https://execute-api.us-east-1.amazonaws.com/prod/LambdaFunction', this.email, { headers: headers })
  .subscribe(data => console.log(data),
    err => console.log(err));

Any idea how to fix this? 任何想法如何解决这个问题?

Looks like you are beating the wrong bush. 好像您在打错灌木丛。

You cannot enable CORS on a 403 since that is sent by APIGateway. 您不能在403上启用CORS,因为这是由APIGateway发送的。 The issue is you are trying to access a URL that is not accessible by APIGateway. 问题是您正在尝试访问APIGateway无法访问的URL。

If your URL is correct and POST worked, you will still receive 200 OK on a CORS call, but you will not be able to access the content received. 如果您的URL正确并且POST有效,那么在CORS呼叫中您仍然会收到200 OK,但是您将无法访问收到的内容。 (That is browser standard to avoid CORS call without valid permission on the return headers) (这是浏览器的标准,以避免在没有返回头的有效许可的情况下进行CORS调用)

I was getting same error and I fixed the issue following path: 我遇到了同样的错误,并通过以下路径解决了该问题:

  • Enable CORS options to add "Access-Control-Allow-Origin": "*" header to your response. 启用CORS选项可在响应中添加“ Access-Control-Allow-Origin”:“ *”标头。 Dont add authonticater to Options resources. 不要将身份验证器添加到选项资源。
  • Add plugin to web browser to overrride cors settings of browser. 将插件添加到Web浏览器以覆盖浏览器的cors设置。 (just for test on browser. you dont need this while app is running on file system) (仅用于在浏览器上进行测试。当应用在文件系统上运行时,您不需要这样做)
  • check your request url/endpoint to be sure is ok. 检查您的请求网址/端点,以确保可以。

Try this first 先尝试一下

  1. Enable CORS options to add "Access-Control-Allow-Origin": "*" header to your response. 启用CORS选项可在响应中添加“ Access-Control-Allow-Origin”:“ *”标头。 Dont add authonticater to Options resources. 不要将身份验证器添加到选项资源。
  2. Add plugin to web browser to overrride cors settings of browser. 将插件添加到Web浏览器以覆盖浏览器的cors设置。 (just for test on browser. you dont need this while app is running on file system) (仅用于在浏览器上进行测试。当应用在文件系统上运行时,您不需要这样做)
  3. check your request url/endpoint to be sure is ok. 检查您的请求网址/端点,以确保可以。

if this is not resolve from above use the below options as well. 如果无法从上方解决此问题,请使用以下选项。 In my case if resolve by below steps. 就我而言,请按以下步骤解决。

  1. Add Access-Control-Allow-Origin: * in Default 4XX response. 在默认4XX响应中添加Access-Control-Allow-Origin:*。 (Refer below Picture.) (请参阅下图。)
  2. Redeploy the API. 重新部署API。

    在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM