API Gateway启用CORS报错:Empty Model不存在及相关问题求助
API Gateway CORS配置报错与解决指南
问题梳理
你遇到的情况:
- 直接在浏览器访问API路由正常,但React用
fetch请求时触发CORS错误:
Access to fetch at 'https://5as8tposvh.execute-api.us-west-2.amazonaws.com/Prod/https://5as8tposvh.execute-api.us-west-2.amazonaws.com/Prod/blast-jobs' from origin 'http://localhost:3000' 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.
- 启用CORS时因未勾选「default gateway response 4xx/5xx」选项,操作后出现「Invalid response status code specified」报错,且无法返回CORS配置页面
报错原因
这个报错和Empty Model不存在无关,核心是CORS配置过程中状态码设置不合法,导致控制台无法正常加载配置页面。而那个「default gateway response 4xx/5xx」选项的作用是:让API Gateway在返回4xx/5xx错误响应时也带上CORS头,避免前端请求出错时依然触发CORS拦截。
解决步骤
1. 恢复可操作状态
- 登录AWS API Gateway控制台,进入你的API的「Resources」页面
- 选中出问题的路由,右键选择「Delete Resource」删除(或点击「Actions」→「Undo Changes」撤销未部署的变更),重新创建该路由
2. 正确配置CORS
- 选中新路由,点击顶部「Actions」→「Enable CORS」
- 在配置窗口中:
- 「Access-Control-Allow-Origin」填写
http://localhost:3000(生产环境建议指定具体域名,不要用*) - 勾选「default gateway response」下的4xx和5xx复选框
- 其他选项保持默认,点击「Enable CORS and replace existing CORS headers」
- 「Access-Control-Allow-Origin」填写
3. 部署API
- 点击「Actions」→「Deploy API」,选择Prod阶段完成部署
4. 修正请求URL
注意到错误信息里的请求URL重复了域名前缀,React中要把请求地址改成正确的:
fetch('https://5as8tposvh.execute-api.us-west-2.amazonaws.com/Prod/blast-jobs')
内容的提问来源于stack exchange,提问作者Joymaker
相关产品推荐
相关产品推荐

