3

我已经使用POST Lambda代理方法和OPTIONS方法为CORS头设置了APIGateway资源。用于Lambda代理的AWS APIGateway CORS不适用

OPTIONS方法返回这些头:当我打电话了POST端点生成的JavaScript SDK

$ curl -i -X OPTIONS https://xxxxxxxxx.execute-api.eu-central-1.amazonaws.com/dev/endpoint1 

HTTP/1.1 200 OK 
Content-Type: application/json 
Content-Length: 0 
Connection: keep-alive 
Date: Sat, 18 Feb 2017 17:07:17 GMT 
x-amzn-RequestId: xxxx 
Access-Control-Allow-Origin: * 
Access-Control-Allow-Headers: Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token 
Access-Control-Allow-Methods: POST,OPTIONS 
X-Cache: Miss from cloudfront 
Via: 1.1 xxxx.cloudfront.net (CloudFront) 
X-Amz-Cf-Id: xxxx== 

然而,Chrome浏览器控制台显示此错误:

XMLHttpRequest cannot load https://xxxx.execute-api.eu-central-1.amazonaws.com/dev/endpoint1. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://localhost:8080' is therefore not allowed access. 

以及火狐:

Cross-Origin Request Blocked: 
The Same Origin Policy disallows reading the remote resource at https://xxxx.execute-api.eu-central-1.amazonaws.com/dev/endpoint1. 
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing). 

为什么m y CORS头部没有考虑到?是否需要更改POST方法设置?

+0

你不能指望CORS一起工作“本地主机”,大概是因为那是假的,你的浏览器知道它。使用'HTTP测试您的网站:// lvh.me'或通过'http:// www.127.0.0.1.xip.io'。这些是localhost的别名,但您的浏览器不知道。 –

回答

5

这似乎需要手动添加头的lambda函数。

在中的NodeJS的情况下的脚本是这样的:

context.succeed({ 
    "statusCode": 200, 
    "headers": { 
     "X-Requested-With": '*', 
     "Access-Control-Allow-Headers": 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,x-requested-with', 
     "Access-Control-Allow-Origin": '*', 
     "Access-Control-Allow-Methods": 'POST,GET,OPTIONS' 
    }, 
    "body": JSON.stringify(response) 
}) 
0

请检查以下要点。

  1. 你部署更新的API?
  2. 你为你的API资源OPTIONS方法?
  3. 你添加的响应头像下面OPTIONS方法的方法应对?
    • 访问控制允许报头
    • 访问控制允许的方法
    • 访问控制允许来源
  4. 你执行你的API资源“启用CORS”行动?
  5. 后完成检查上述情况,请检查GET/POST方法的API资源的方法请求。也许,Access-Control-Allow-Origin HTTP标头是由API网关自动添加的。

感谢, 丹尼尔

相关问题