我已经使用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方法设置?
你不能指望CORS一起工作“本地主机”,大概是因为那是假的,你的浏览器知道它。使用'HTTP测试您的网站:// lvh.me'或通过'http:// www.127.0.0.1.xip.io'。这些是localhost的别名,但您的浏览器不知道。 –