Rails 4 を使用して一連のサービスを作成しています。これは、JavaScript ブラウザー アプリケーションで使用しています。クロスオリジン GETS は正常に動作していますが、私の POST は 404 エラーでプリフライト OPTIONS チェックに失敗しています。少なくとも、私はそれが起こっていると思います。コンソールに表示されるエラーは次のとおりです。これは Mac 上の Chrome 31.0.1650.63 です。
OPTIONS http://localhost:3000/confessor_requests 404 (Not Found) jquery-1.10.2.js:8706
OPTIONS http://localhost:3000/confessor_requests No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. jquery-1.10.2.js:8706
XMLHttpRequest cannot load http://localhost:3000/confessor_requests. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. main.html:1
CORS を有効にするための手順を高低で検索しましたが、困惑しています。通常の推奨事項は、私が行ったアプリケーションコントローラーにこのようなものを配置することです。
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = "1728000"
end
def cors_preflight_check
if request.method == :options
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
OPTIONS リクエストが入ったときにこのアクションにリダイレクトする、routes.rb のある種のルートが続きます。
match "/*all" => "application#cors_preflight_check", :constraints => { :method => "OPTIONS" }
'match' ディレクティブは Rails 4 では機能しなくなったので、次のように POSTS と直接一致させようと試みました。
post "/*all" => "application#cors_preflight_check", :constraints => { :method => :options }
しかし、それでもうまくいきません。GET リクエストが機能しているので、私が見逃しているのは OPTIONS リクエストの正しいルートだと思います。ただし、考えられるすべてのルートを試しましたが、リクエストを通過させるものは何もないようです。
cyu/rack-corsもインストールしてみましたが、同じ結果が得られました。
私が間違っていることを知っている人はいますか?