我已经使用POST Lambda Proxy方法和CORS header 的OPTIONS方法设置了APIGateway资源。
OPTIONS方法返回以下 header :

$ 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==

但是,当我使用生成的Javascript SDK调用POST端点时,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.

以及Firefox:
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).

为什么不考虑我的CORS header ?是否需要对POST方法设置进行其他更改?

最佳答案

似乎需要在lambda函数中手动添加 header 。

对于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)
})

关于javascript - 适用于Lambda代理的AWS APIGateway CORS不适用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42318174/

10-09 23:39