AWS API Gateway与Lambda配置CORS后仍报错问题咨询
AWS API Gateway + Lambda 跨域(CORS)问题排查
我使用AWS API Gateway和Lambda搭建API服务,前端发起请求时遭遇CORS跨域拦截。检查配置时发现:尽管已在API Gateway中为所有方法启用CORS,但Lambda触发器配置区域内,对应API Gateway端点的CORS字段显示为NO。
现有配置详情
API Gateway CORS配置
[API Gateway CORS配置截图]
Lambda 响应头中的CORS配置代码
export const handler = async (event, context) => { let body; let response; let statusCode = 200; const headers = { "Access-Control-Allow-Origin": "https://www.yoteinvito.com", "Access-Control-Allow-Headers": "Content-Type, Accept", "Access-Control-Allow-Methods": "POST, GET, PUT, OPTIONS", "Access-Control-Allow-Credentials":"true", };
Lambda触发器配置
[Lambda触发器配置页面]
前端请求头配置
headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(formData), })
浏览器控制台错误信息
Access to fetch at 'https://rgfz15v2qp.execute-api.us-east-2.amazonaws.com/dev/confirmations' from origin 'https://www.yoteinvito.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: 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.
疑问与求助
我已在API Gateway中为所有方法(GET、POST、PUT、DELETE等)启用CORS,添加了必要响应头并配置了OPTIONS请求的响应规则,但仍出现上述跨域错误。想咨询:
- API Gateway端点详情中显示
CORS: NO是否属于正常情况? - 该显示是否是导致CORS报错的直接原因?
- 还需执行哪些操作才能确保CORS完全生效?
内容的提问来源于stack exchange,提问作者Angel Hernadez
相关产品推荐
相关产品推荐

