'Connection Refused Error on React

I'm trying to connect my react app to a rails api, however, i can't seem to get the data. The error message is

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

Fetch Error :-S TypeError: Failed to fetch.

I just have JSON going in in localhost//3000 served up by rails.

My JS code:

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);
  });

I thought the solution to this was to install the rack cors gem and set up the middleware in application.rb but it's not working. Thanks.



Solution 1:[1]

The API route seems odd. There should not be double slashes in a URL other than the protocol. Try changing it to fetch('http://localhost:3000')

Firstly, try to visit http://localhost:3000 in your browser and make sure your server side is returning the data correctly. It might be due to a problem on your server too.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yangshun Tay