【发布时间】: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