Rails 6, React, Rack-Cors

2.4k Views Asked by At

I have a react app running on localhost:3001 and a rails 6 api backend on localhost:3000

I keep getting cors errors when I post from react to rails. The rails app has Rack Cors.

I've tried adding this to cors.rb in the initializers folder.

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: :any
  end
end

It didn't work, I also tried to add it to application.rb and config.ru. Noting has worked, and I am confused.

1

There are 1 best solutions below

3
fazlu On
allow do
 origins '*'
 resource '*', :headers => :any, :methods => :any
end

if this will not work try putting

origins 'localhost:3001'

Hope it helps