无法在ReactJS上从Rails提取数据

问题描述 投票:0回答:2

我使用的前端源是端口5555上的ReactJS,后端是端口8888上的Rails。我试图通过使用以下命令从React的Rails中获取数据:

const url = 'http://localhost:8888/problems';
fetch(url)
  .then(res => {
    res.json();
  })
  .then(response => {
    console.log(response);
  })
  .catch(error => {
    console.log(error);
  });

但是收到了我的消息:

Access to fetch at 'http://localhost:8888/problems' from origin 'http://localhost:5555' 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.

我不知道该错误。请帮助我修复它

ruby-on-rails reactjs fetch-api
2个回答
0
投票

正如评论所说,这是一个CORS错误。

您可以根据此问题allow-anything-through-cors-policy修复它>


0
投票

阅读有关CORS的更多信息

© www.soinside.com 2019 - 2024. All rights reserved.