ruby on rails - Allow anything through CORS Policy -
how can disable cors? reason wild carded allowed origins , headers yet ajax requests still complain origin not allowed cors policy....
my applications controller :
class applicationcontroller < actioncontroller::base protect_from_forgery before_filter :current_user, :cors_preflight_check after_filter :cors_set_access_control_headers # responses in controller, return cors access control headers. def cors_set_access_control_headers headers['access-control-allow-origin'] = '*' headers['access-control-allow-methods'] = 'post, get, options' headers['access-control-allow-headers'] = '*' headers['access-control-max-age'] = "1728000" end # if preflight options request, short-circuit # request, return necessary headers , return empty # text/plain. def cors_preflight_check if request.method == :options headers['access-control-allow-origin'] = '*' headers['access-control-allow-methods'] = 'post, get, options' headers['access-control-allow-headers'] = '*' headers['access-control-max-age'] = '1728000' render :text => '', :content_type => 'text/plain' end end private # user logged in def current_user @current_user ||= user.find(session[:user_id]) if session[:user_id] end helper_method :current_user end
routes:
match "*all" => "application#cors_preflight_check", :constraints => { :method => "options" } match "/alert" => "alerts#create" match "/alerts" => "alerts#get" match "/login" => "sessions#create" match "/logout" => "sessions#destroy" match "/register" => "users#create"
edit---
i tried:
config.middleware.use rack::cors allow origins '*' resource '*', :headers => :any, :methods => [:get, :post, :delete, :put, :options] end end
in application.rb
--edit 2---
the problem chrome extensions may not support cors think. how can fetch information bypassing cors? how should respond preflight check?
i've same requirements on public api used rails-api.
i've set header in before filter. looks this:
headers['access-control-allow-origin'] = '*' headers['access-control-allow-methods'] = 'post, put, delete, get, options' headers['access-control-request-method'] = '*' headers['access-control-allow-headers'] = 'origin, x-requested-with, content-type, accept, authorization'
it seems missed access-control-request-method header.
Comments
Post a Comment