Nik*_*ntz 2 python cors amazon-cognito aws-lambda serverless-framework
我从 Angular 前端收到此错误消息,但我无权接触我的 lambda 代码:
`Access to fetch at 'https://testapicd.***.***.com/localization/v1/role' from origin 'https://localization.test.***.***.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.`
Run Code Online (Sandbox Code Playgroud)
我到处找,我的代码中似乎没有错误。我的无服务器代码是
getrole:
handler: v1/handler_get_role.get_role
name: get_role
events:
- http:
path: v1/role
method: get
cors: true
authorizer:
name: CognitoCSAuthorizer
type: COGNITO_USER_POOLS
arn: ${file(config.${self:provider.stage}.json):userpoolarn}
Run Code Online (Sandbox Code Playgroud)
我已经三重检查了所有设置,一切似乎都正确。有什么建议该怎么做?该功能在开发环境中有效,但在我将其部署到测试环境时无效。
如果我直接针对 API 尝试令牌,那么它也不起作用(但在开发中运行良好)。我什至不再相信这是 CORS 问题。我认为 jwt 令牌是错误的。
def get_role(event, context):
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin' : '*', # Required for CORS support to work
'Access-Control-Allow-Credentials': 'true',
},
'body': json.dumps("TEST")
}
Run Code Online (Sandbox Code Playgroud)
之前我已经与这个问题搏斗了几个小时(如果不是几天),结果证明我不仅必须在serverless.yml文件上启用 cors,而且还必须在从 Lambda 返回的对象中添加响应标头作为属性。
像这样的事情应该这样做:
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify({
product: product
}),
};
Run Code Online (Sandbox Code Playgroud)
这篇文章当时救了我的命,我希望它能救你的命!
| 归档时间: |
|
| 查看次数: |
1977 次 |
| 最近记录: |