【发布时间】:2019-01-28 05:40:28
【问题描述】:
如何在 Serverless 中启用 CORS?
我在调用我的 lambda 函数时不断收到以下错误消息。
No 'Access-Control-Allow-Origin' header is present on the requested resource.
我已经到处搜索了,但我仍然无法弄清楚如何解决它。任何帮助将不胜感激!!!
这是我的设置:
我的 Python 处理程序,用 serverless invoke local 测试过
def main(event, context):
...operations with pandas...
return {
'statusCode': 200,
'headers': {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': True
},
'body': json.dumps(response),
}
我的 serverless.yml
functions:
main:
handler: handler.main
events:
- http:
path: dashboard
method: get
authorizer: aws_iam
cors: true
我的前端,一个 React 应用程序。 API 调用是使用 aws-amplify 进行的:
try{
const myInit = {
body: JSON.stringify(this.props.session),
headers: {
"Content-Type": "application/json"
}
};
const calculation = await API.get("clearly", "/dashboard", myInit)
console.log(calculation);
} catch(e) {
...
}
谢谢!
编辑:
当我尝试在 API Gateway 中手动启用 CORS 时,我收到了 Invalid Response status code specified 错误消息。
Capture
【问题讨论】:
标签: cors aws-api-gateway serverless-framework aws-amplify