ajax - Allow CORS in Ruby on Rails -


in config/application.rb file, have code,

config.action_dispatch.default_headers = {         'access-control-allow-origin' => '*',         'access-control-request-method' => 'get, patch, put, post, options, delete'     } 

but not allow me send post request route on sever

safari gives error:

http://localhost:3000/studentsfailed load resource: server responded status of 404 (not found) http://localhost:3000/studentsfailed load resource: origin http://localhost:4200 not allowed access-control-allow-origin. localhost:1xmlhttprequest cannot load http://localhost:3000/students. origin http://localhost:4200 not allowed access-control-allow-origi 

and in rails server console:

started options "/students" ::1 @ 2015-03-28 21:00:45 -0500  actioncontroller::routingerror (no route matches [options] "/students"): 

i spent time working on , can tell reliable solution use rack-cors. see: https://github.com/cyu/rack-cors

first add gem:

gem 'rack-cors', '~> 0.3.1' 

then in application.rb add

config.middleware.insert_before 0, "rack::cors"   allow     origins '*'     resource '*', :headers => :any, :methods => [:get, :post, :options]   end end 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -