首页 > 解决方案 > ReactJS Axios 外部 API CORS

问题描述

我在我的 React 项目中使用外部 API
但是我在那里遇到了 CORS 错误
当我在谷歌搜索时,有很多文章建议在后端服务器上设置 CORS
但在我的情况下,我使用的是外部 API,我可以'不控制他们的后端添加 CORS
我可以在前端或浏览器内部禁用它而不接触后端吗?
这是错误代码

Access to XMLHttpRequest at 'https://externaldomain.com' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

标签: javascriptreactjsxmlrestcors

解决方案


如果 API 未配置正确的 CORS 设置,并且它不是您的 API,则其中一种解决方案是使用配置为处理具有正确 CORS 标头的请求的代理。

它看起来像这样:

cors图

您可以配置自己的代理,也可以像这样使用现成的代理(请注意,此代理的作者要求不要过度使用,所以如果它是一个大型应用程序,您应该自己设置):

https://thingproxy.freeboard.io/fetch/https://externaldomain.com

推荐阅读