Rails Responds with 404 on CORS Preflight Options Request

前端 未结 7 1343
既然无缘
既然无缘 2020-12-10 00:42

I\'m creating a set of services using Rails 4, which I am consuming with a JavaScript browser application. Cross-origin GETS are working fine, but my POSTs are failing the p

7条回答
  •  甜味超标
    2020-12-10 00:56

    Working on Rails 3.2.11.

    I put

    match '*path', :controller => 'application', :action => 'handle_options_request', :constraints => {:method => 'OPTIONS'}
    

    in my routes.rb file. The key was to put it as top priority (on top of the routes.rb file). Created that action so that it is publicly available:

      def handle_options_request
        head(:ok) if request.request_method == "OPTIONS"
      end
    

    And a filter in application controller:

     after_filter :set_access_control_headers
    
      def set_access_control_headers
        headers['Access-Control-Allow-Origin'] = '*'
        headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE'
      end
    

提交回复
热议问题