我正在使用无服务器框架来创建一个被调用的api,并生成一个返回到我的网站的字符串。虽然检索过程在脱机模式下都有效,当我从AWS API端点调用该功能时,当我从网站发出请求时,我总是收到CORS错误。我按照guide进行了配置python设置的cors,但是仍然出现错误,无法确定它的来源以及是否应该更改代码或手动修改API网关。 / p>
这是客户端错误:
Access to XMLHttpRequest at 'https://*id*.execute-api.us-east-1.amazonaws.com/prod/test' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
localhost:8080
Axios请求:
axios.get('https://*id*.execute-api.us-east-1.amazonaws.com/prod/test')
代码:
from lambda_decorators import cors_headers
@cors_headers
def test(event, context):
response = {
"statusCode": 200,
"body": {
"test_url": 'https://www.google.com'
}
}
return response
serverless.yml:
functions:
test:
handler: callback.test
events:
- http:
path: /test
method: get
cors: true
sls invoke -f test --stage prod
:
{
"statusCode": 200,
"body": {
"test_url": "https://www.google.com"
"headers": {
"Access-Control-Allow-Origin": "*"
}
}
API网关配置(GET方法):
集成请求:LAMBDA_PROXY
方法响应:200