javascript - API Gateway failing to enable CORS - Stack Overflow

When I attempt to request a lambda-backed API (using API gateway, deployed using the CLI and Cloud Deve

When I attempt to request a lambda-backed API (using API gateway, deployed using the CLI and Cloud Development Kit) from my react app, I get the following error:

Access to XMLHttpRequest at '' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

GET  net::ERR_FAILED

My API resources defined using the CDK are all passed into this method

When I attempt to request a lambda-backed API (using API gateway, deployed using the CLI and Cloud Development Kit) from my react app, I get the following error:

Access to XMLHttpRequest at 'https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws./prod/xxxxx' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

GET https://xxxxxxxxxx.execute-api.eu-west-1.amazonaws./prod/xxxxx net::ERR_FAILED

My API resources defined using the CDK are all passed into this method

Share Improve this question asked Aug 18, 2019 at 19:58 wllklewllkle 371 silver badge6 bronze badges 3
  • this may seem like a silly question, but did you enable CORS in API Gateway and re-deploy the API? – danimal Commented Aug 18, 2019 at 21:05
  • @danimal yes I have tried that, but the problem is that should already be taken care of by the CDK. – wllkle Commented Aug 18, 2019 at 21:14
  • Are you able to provide a snippet of the CDK code you are using to deploy the API? – Icehorn Commented Aug 19, 2019 at 5:39
Add a ment  | 

1 Answer 1

Reset to default 8

Like explain here, you need to enable CORS in API Gateway, BUT you also need to return an Access-Control-Allow-Origin header from your Lambda because API Gateway doesn't add that automatically to the responses.

Here is a sample of what my Lambda return for a simple Get :

return {
  headers,
  body: JSON.stringify(response.Item),
  statusCode: 200
};

const headers = {
  "Access-Control-Allow-Origin": "*", // Required for CORS support to work
  "Access-Control-Allow-Credentials": true // Required for cookies, authorization headers with HTTPS
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742312098a4420097.html

相关推荐

  • javascript - API Gateway failing to enable CORS - Stack Overflow

    When I attempt to request a lambda-backed API (using API gateway, deployed using the CLI and Cloud Deve

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信