无服务器框架 AWS REST API 网关 - 403 CORS 错误
serverless framework AWS REST API Gateway - 403 CORS error
我正在尝试创建一个启用了 CORS 的函数。
该函数使用 Postman(存在 cors headers)按预期工作,但从浏览器尝试时出现 CORS 错误(无 cors header)。
const getTicket = async event => {
var ticketNumber = Date.now();
return {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
body: JSON.stringify({"ticketNumber": ticketNumber})
};
};
functions:
getTicket:
handler: code/common/ticket.getTicket
runtime: nodejs12.x
events:
- http:
method: get
path: getTicket
cors: true
authorizer:
arn: ${self:custom.auth0.authorizer_arn}
我还尝试了其他几种编写 serverless.yaml 文件的方法,但其中 none 有效,它们之间的唯一区别是在我的 API 网关中创建的方法。有时我得到 GET+OPTIONS 方法,有时只有 GET,但从未使用过 CORS。
我收到错误:
"Access to XMLHttpRequest at 'https://...amazonaws.com/prod/getTicket' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
您可能看到的是服务器在到达您的 return 语句之前 returning 一个错误。换句话说,API 网关正在 returning 502 或 403(因为这些是最常见的错误),而那些特定的 return 没有 CORS headers默认为 API 网关。我建议使用 CloudWatch 检查特定的 Lambda 调用以确定您的 Lambda 函数本身是否出错并纠正它。
您还可以强制您的 API 网关为所有响应 return CORS headers。在 serverless.yml 文件的 resources
部分,您可以执行以下操作并为其他状态代码添加任何附加条目:
resources:
Resources:
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'
我正在尝试创建一个启用了 CORS 的函数。 该函数使用 Postman(存在 cors headers)按预期工作,但从浏览器尝试时出现 CORS 错误(无 cors header)。
const getTicket = async event => {
var ticketNumber = Date.now();
return {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': '*',
'Access-Control-Allow-Origin': '*',
},
statusCode: 200,
body: JSON.stringify({"ticketNumber": ticketNumber})
};
};
functions:
getTicket:
handler: code/common/ticket.getTicket
runtime: nodejs12.x
events:
- http:
method: get
path: getTicket
cors: true
authorizer:
arn: ${self:custom.auth0.authorizer_arn}
我还尝试了其他几种编写 serverless.yaml 文件的方法,但其中 none 有效,它们之间的唯一区别是在我的 API 网关中创建的方法。有时我得到 GET+OPTIONS 方法,有时只有 GET,但从未使用过 CORS。
我收到错误: "Access to XMLHttpRequest at 'https://...amazonaws.com/prod/getTicket' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."
您可能看到的是服务器在到达您的 return 语句之前 returning 一个错误。换句话说,API 网关正在 returning 502 或 403(因为这些是最常见的错误),而那些特定的 return 没有 CORS headers默认为 API 网关。我建议使用 CloudWatch 检查特定的 Lambda 调用以确定您的 Lambda 函数本身是否出错并纠正它。
您还可以强制您的 API 网关为所有响应 return CORS headers。在 serverless.yml 文件的 resources
部分,您可以执行以下操作并为其他状态代码添加任何附加条目:
resources:
Resources:
GatewayResponseDefault4XX:
Type: 'AWS::ApiGateway::GatewayResponse'
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
ResponseType: DEFAULT_4XX
RestApiId:
Ref: 'ApiGatewayRestApi'