Can't get rack-cors working in rails application

后端 未结 6 1254
野的像风
野的像风 2020-12-12 19:47

I wanted to implement CORS in my rails application, so I googled rack-cors gem for it. And I did everything as was said in README, that is updated Gemfile accordingly and up

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 20:07

    I had to create a special route to handle the options requests, the cors gem didn't do it for me like I expected it to. The route I added to the end of routes.rb was:

      match "*path", :to => proc {|env| [200, {
      'Access-Control-Allow-Origin' => '*',
      'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Credentials' => 'true',
      'Access-Control-Request-Method' => '*',
      'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
    'Content-Type' => 'text/plain'
    
     }, ["CORS Preflight"]] }, :via => [:options]
    

提交回复
热议问题