简体   繁体   English

ReactJS Axios 外部 API CORS

[英]ReactJS Axios External API CORS

I'm using external API inside my React Project我在我的 React 项目中使用外部 API
But i got CORS Error there但是我在那里得到了 CORS 错误
When i'm googling, there are many article suggest to setup the CORS at the backend server当我谷歌搜索时,有很多文章建议在后端服务器上设置 CORS
But in my case, i'm using external API which is i can't control their backend to add the CORS there但就我而言,我使用的是外部 API,这是我无法控制他们的后端在其中添加 CORS
Can i disable it on the frontend or inside the browser only without touching the backend?我可以在不接触后端的情况下仅在前端或浏览器内部禁用它吗?
This is the Error code这是错误代码

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.

If the API is not configured with the correct CORS settings, and it is not your API, one of the solutions is to use a proxy that is configured to handle the request with the correct CORS headers.如果 API 没有配置正确的 CORS 设置,并且它不是您的 API,解决方案之一是使用配置为处理具有正确 CORS 标头的请求的代理。

It will look like this:它看起来像这样:

cors图

You can configure your own proxy or you can use a ready one like this (please note that the author of this proxy asks not to overuse is so if it is a big application you should set your own):您可以配置自己的代理,也可以像这样使用现成的代理(请注意,此代理的作者要求不要过度使用,因此如果它是一个大型应用程序,您应该设置自己的代理):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM