在 AWS Lambda 上配置 CORS 响应标头? [英] Configure CORS response headers on AWS Lambda?
问题描述
我正在尝试使用 AWS API Gateway 创建一个新服务,但我发现浏览器会自动调用 OPTIONS 方法以获取 CORS 信息.
问题在于 AWS API Gateway 不提供配置 CORS 标头的本机方式.
是否可以创建一个 Lambda 脚本来响应 OPTIONS 方法?
如果您使用 {proxy+}
端点,则必须在 Lambda 函数中处理 CORS HTTP 请求.实现取决于您使用的框架.对于 Express,最简单的解决方案是简单地使用 Express CORS 中间件..>
如果您不想通过 Lambda
处理 CORS
请求,请尝试更改您的 Lambda 方法
的设置以处理 API 网关
级别的 CORS.
在 Access-Control-Allow-Headers
中允许标题 X-Api-Key
也很重要,否则 auth 将不起作用,你会得到错误.
编辑:2015 年 11 月,API Gateway 团队添加了一项新功能以简化 CORS 设置.
I'm trying to create a new service using AWS API Gateway, but I found out the browser automatically calls OPTIONS method in order to obtain CORS information.
The problem is that AWS API Gateway does not offer a native way to configure CORS headers.
Is it possible to create a Lambda Script in order to respond to OPTIONS method?
If you're using {proxy+}
endpoint, you must handle CORS HTTP requests in the Lambda function. The implementation depends on the framework you're using. For Express, the easiest solution is to simply use Express CORS middleware.
If you don't want to handle CORS
requests by Lambda
, try changing the settings of your Lambda Method
to handle CORS
on the API Gateway
level.
Here's a detailed official tutorial for CORS setup on AWS API Gateway.
It's also critical that you allow header X-Api-Key
in Access-Control-Allow-Headers
otherwise auth won't work and you'll get errors.
EDIT: In November 2015 the API Gateway team added a new feature to simplify CORS setup.
这篇关于在 AWS Lambda 上配置 CORS 响应标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!