从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" }

内容的提问来源于stack exchange,提问作者Ash
相关产品推荐
相关产品推荐

