No Access-Control-Allow-Origin' header is present on the requested resource error in react-redux desktop application [关闭]

x6yk4ghg  于 2023-08-05  发布在  React
关注(0)|答案(2)|浏览(104)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答这个问题。
7天前关闭
Improve this question
我从API获取数据,但它不工作。它给了我这样的错误:
访问'http‍://muslimsalat.com/lahore/false/5.json?key=API_KEY' from origin ' http://localhost:3000 '已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'Access-Control-Allow-Origin'标头。如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”,以便在禁用CORS的情况下获取资源。
当我在react-native移动的中获取数据时,它可以工作,但当我在react desktop中获取数据时,它不工作。
谁能告诉我这个错误的原因是什么,我该如何解决?

mspsb9vt

mspsb9vt1#

将CORS添加到API URL中。您只需将本地IP添加到后端服务(API)。如果您需要适当的代码,让我们知道您使用的后端API语言。

smtd7mpg

smtd7mpg2#

CORS是一种机制,域可以指定哪些 * 其他 * 域可以向它发出请求。默认情况下,没有Access-Control-Allow-Origin头,这意味着例如stackoverflow.com处的脚本不能调用google.com

fetch('https://www.google.com')
  .then(console.log)
  .catch(console.error)
// Error: Failed to fetch

字符串
这就是为什么作为脚本运行的React应用程序无法获取Web上的大多数内容。

然而,React Native应用 * 作为应用 * 运行,而不是作为脚本运行,因此不受CORS的影响。

相关问题