我有两个CloudFront发行版:
JS代码托管在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 Behavior的Access-Control-Request-Headers
的白名单中添加Access-Control-Request-Method
,Origin
和Cache Based on Selected Request Headers
。还是没有运气
答案 0 :(得分:0)
通过在Lambda函数响应中添加Header消息解决了此问题。 与此类似:
response["headers"] = {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
}
参考:https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html