我有两个 CloudFront 分配:
- 一个使用 S3 作为源 - 路由53 域 - example.com
- 第二个使用 API 网关 URL 作为源。 - 路由53域 - api.com
JS Code 托管在 S3 和 First Cloudfront Distribution。 https://example.com/test.html
正在打电话给https://api.com/api?<queryParameters>
但是我收到错误说: Access to XMLHttpRequest at 'https://api.com/api?<queryParameters>' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我尝试在 API CloudFront 行为的Cache Based on Selected Request Headers
白名单中添加Access-Control-Request-Headers
、Access-Control-Request-Method
和Origin
。还是没有运气
此问题已通过在 Lambda 函数响应中添加标头消息得到解决。类似这个:
response["headers"] = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
参考: https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html