You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从JS Fetch访问AWS API Gateway→Lambda时遇CORS问题求助

AWS API Gateway + Lambda 跨域(CORS)问题排查

社区里相关问题不少,但我试了各种方法都没解决。情况如下:我在AWS API Gateway后部署了Node.js Lambda,用Postman调用完全正常,但在简单的HTML+JS网站中使用fetch调用时,触发了CORS错误:

Access to fetch at 'https://......' from origin 'https://......' 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.

我已尝试的解决措施:

  • 创建Lambda函数URL,排除API Gateway的复杂性
  • 添加OPTIONS请求处理器,该处理器可正常工作且已配置对应CORS头
  • 在响应输出中添加以下CORS头,且能在Postman的响应中看到:
{
  "Content-Type":"application/json",
  "Access-Control-Allow-Headers" : "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token",
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Methods": "OPTIONS,POST,GET"
}

Postman响应中的CORS头

内容的提问来源于stack exchange,提问作者Ash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:42:38