cors - 无服务器启用 CORS
问题描述
如何在无服务器中启用 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
错误消息。
捕获
解决方案
您是否使用模拟集成类型创建了 OPTIONS 方法以返回像这样的响应标头
Access-Control-Allow-Methods Access-Control-Allow-Headers Access-Control-Allow-Origin
只需参考(https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html)
推荐阅读
- mysql - 如何将逗号分隔值传递给 MySql 类型数组中的存储过程?
- python - 我正在尝试通过在 python 中使用烧瓶来创建端点,但我得到 TypeError: 'str' object is not callable
- python - 入口 tkinter 上的验证器
- node.js - 什么是Ejs,EJS有什么用?
- java - 使用 Hibernate 持久化和查询多对象关系
- javascript - Discord.js 'ReferenceError: 要求未定义'
- python - 按键类型解构 json
- r - 将一列列表添加到 R 中的数据框
- node.js - Nodejs Swagger 无法向请求添加授权标头
- html - 如何制作内容居中的可点击响应方块?