0

I'm working on a Rails 5.0.6 as an API. I am also running a React application for the front end running on a node server version v9.8.0. On the React app which is running on localhost:3000 I get the following error:enter image description here

The rails is used as an API so in the controllers I return @drinks in json format.

drinks_controller.rb

class DrinksController < ApiController
  # GET /drinks
  def index
    @drinks = Drink.select("id,title").all

    render json: @drinks.to_json
  end

  # GET /drinks/1
  def show
    @drink = Drink.find(params[:id])
    render json: @drink.to_json(:include => { :ingredients => { :only => [:id, :description] }})
  end
end

I run both servers locally using Procfile in Profile.dev

web: cd client && PORT=3000 npm start
api: PORT=3001 && bundle exec rails s

When I go to the rails server which is running on localhost:3001/api/drinks I get the following:

[{"id":1,"title":"Sparkling Negroni"},{"id":2,"title":"Pineapple-Jalapeño Margarita"}]

which is in json format, On the app.js im fetching from that endpoint

class App extends Component {
  componentDidMount() {
    window.fetch('api/drinks')
      .then(response => response.json())
      .then(json => console.log(json))
      .catch(error => console.log(error))
  }

Is it the proxy not working?

{
  "name": "package-json",
  "version": "4.0.1",
  "description": "Get metadata of a package from the npm registry",
  "license": "MIT",
  "repository": "sindresorhus/package-json",
  "proxy": "http://localhost:3001",
  "author": {
    "name": "Sindre Sorhus",
    "email": "[email protected]",
    "url": "sindresorhus.com"
  }

I can't understand what the issue is?

enter image description here

5
  • I see that you are using the developer toolbar, what is going on in your network panel? What response is returned by your server? Commented Mar 22, 2018 at 2:16
  • @JoseJimenez I updated the post with an image of the network Commented Mar 22, 2018 at 2:24
  • Just to be sure, try window.fetch('/api/drinks') Commented Mar 22, 2018 at 2:35
  • @StevenAguilar you can click on your /api/drinks line in the network panel and look for a "response" or "preview" tab in the resulting screen. It should show you what is coming back from your rails applicaiton. Commented Mar 22, 2018 at 13:11
  • 1
    Possible duplicate of "SyntaxError: Unexpected token < in JSON at position 0" in React App Commented Mar 22, 2018 at 21:32

1 Answer 1

2

I suspect your rails app is returning html when fetch is expecting JSON... The < character is indicative that an HTML string is being delivered, (the JSON parser is choking on HTML).

Try dumping the response "as is" to see what you're receiving.

class App extends Component {
  componentDidMount() {
    window.fetch('api/drinks')
      .then(response => {
         console.log(response.status);
         console.log(response.body);
         return response.json();
      })
      .then(json => console.log(json))
      .catch(error => console.log(error))
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.