【问题标题】:Connection Refused Error on ReactReact 上的连接被拒绝错误
【发布时间】:2017-08-18 21:34:36
【问题描述】:

我正在尝试将我的 react 应用程序连接到 rails api,但是,我似乎无法获取数据。错误信息是

GET http://localhost//3000 net::ERR_CONNECTION_REFUSED

Fetch Error :-S TypeError: 获取失败。

我只是将 JSON 输入到 localhost//3000 中,由 rails 提供。

我的 JS 代码:

fetch('http://localhost://3000')
  .then(
    function(response) {
      if (response.status !== 200) {
        console.log('Looks like there was a problem. Status Code: ' +
          response.status);
        return;
      }

      // Examine the text in the response
      response.json().then(function(data) {
        console.log(data);
      });
    }
  )
  .catch(function(err) {
    console.log('Fetch Error :-S', err);
  });

我认为解决方案是安装 rack cors gem 并在 application.rb 中设置中间件,但它不起作用。谢谢。

【问题讨论】:

  • 在服务器上运行 js 代码时出现什么错误
  • http://localhost://3000 我觉得这里打错了,应该是http://localhost:3000

标签: ruby-on-rails json api reactjs


【解决方案1】:

API 路由看起来很奇怪。协议以外的 URL 中不应有双斜杠。试试改成fetch('http://localhost:3000')

首先,尝试在您的浏览器中访问http://localhost:3000,并确保您的服务器端正确返回数据。也可能是您的服务器出现问题。

【讨论】:

  • 哦,这只是一个错字。没关系。我想到了。必须在我的 package.json 上设置 localhost:3000 作为代理
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 2018-04-21
  • 2016-04-25
相关资源
最近更新 更多