2

Error: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Backund Ruby on Rails, Frond-end Angular 5.

application.rb

...
module Dkeeper
  class Application < Rails::Application
    config.middleware.use Rack::Cors do
      allow do
      origins '*'
      resource '*', headers: :any, methods: [:get, :post, :put, :patch, :delete, :options, :head]
    end
end
...
andrea2010
  • 338
  • 2
  • 15

1 Answers1

1

Looks like you're missing insert_before 0 according to the docs

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :options]
  end
end
Dan Hilton
  • 199
  • 7
  • When I use insert_before 0, I get an error another character, or rather the page code. – andrea2010 Jul 13 '18 at 11:09
  • Here such, only it abbreviated. Error: Uncaught (in promise): Action Controller: Exception caught – andrea2010 Jul 13 '18 at 11:12
  • Any other hints in the error, maybe in the rails console? Are you able to see the 'Access-Control-Allow-Origin' header in the response when you open up dev tools and check out network requests? – Dan Hilton Jul 13 '18 at 11:17
  • The browser console has: Request URL: https://*.herokuapp.com/api/users Request Method: POST Status Code: 500 Internal Server Error Remote Address: ******* Referrer Policy: no-referrer-when-downgrade ... On the local server, I have everything working, the problem is when I upload to Heroku. – andrea2010 Jul 13 '18 at 11:21
  • By the way, you are right, the header is back, but still, what do I do with this new error, why do I get the application code? – andrea2010 Jul 13 '18 at 11:29
  • Can you run `heroku logs --tail` and make the request to see if any server side errors show up? – Dan Hilton Jul 13 '18 at 11:43
  • It might be an order thing can you verify that `bundle exec rake middleware` is the same on local and heroku? https://stackoverflow.com/a/20465250/9430067 seems to move the config to config.ru, not sure if that would help but might be worth trying. – Dan Hilton Jul 13 '18 at 11:45
  • Logs during registration - 2018-07-13T11: 48: 24.637290 + 00: 00 heroku [router]: at = info method = POST path = "/ api / users" host = *.herokuapp.com request_id = f249ad32 -95f7-4a83-b0f4-d0f40cef16cc fwd = "176.98.73.167" dyno = web.1 connect = 1ms service = 480ms status = 500 bytes = 103602 protocol = https, and your team did not help. – andrea2010 Jul 13 '18 at 11:50
  • any difference in ordering between `bundle exec rake middleware` and `heroku run rake middleware`? – Dan Hilton Jul 13 '18 at 12:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174942/discussion-between-andrea2010-and-dan-hilton). – andrea2010 Jul 13 '18 at 12:05