0

I'm trying to send a post method using jquery

  jQuery.ajax({
      headers: { 'Authorization': "Token token=123546" },
      url: "https://paycar-public-api.herokuapp.com/api/v1/users", 
      type: "POST",
      processData: false,
      contentType: 'application/json',
      data:  
          JSON.stringify({
          email: "mikew75+123@gmail.com",

        })
  });

I get

Failed to load https://paycar-public-api.herokuapp.com/api/v1/users: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://app.instapage.com' is therefore not allowed access. The response had HTTP status code 500.

my application.rb looks like this :

class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true

# CORS
config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end
end

the API is hosted with heroku

I'm not sure I understand why it's not working, any help would be greatly appreciated.

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Mike W
  • 391
  • 4
  • 14
  • *The response had HTTP status code 500* — that’s the problem you need to fix. Unrelated to your CORS config, some internal server error is happening and you need to first get that figured out and fixed. Check your server logs. The only reason you’re seeing mention of Access-Control-Allow-Origin in that browser message is because headers you set in your application code don’t get added to 5xx responses. – sideshowbarker Jan 11 '18 at 23:13

2 Answers2

0

Mike,

Checkout this gem to see if it helps you get around the issue: https://github.com/cyu/rack-cors

To learn more about why it's occurring you can check out some of the previously answered questions:

How does Access-Control-Allow-Origin header work?

iceveda06
  • 601
  • 8
  • 21
  • already installed. I can't see why it's rejecting me even though I've enabled get post, and options from everywhere – Mike W Jan 11 '18 at 14:47
  • Looks like you figured it out. I ran into CORS error as well and rack-cors config got me around it. – iceveda06 Jan 11 '18 at 17:16
0

Actually, it was a simple mistake with my rails setup. it was in staging and the configuration was buggy.

Mike W
  • 391
  • 4
  • 14