LoginSignup
19
20

More than 5 years have passed since last update.

Ruby on Rails とReact.js で作るWEBアプリケーションのクロスドメイン対応

Last updated at Posted at 2015-08-26

課題

RailsのサーバーをAPIサーバー化して、フロントをReact.jsで構築した際にhttpリクエストを投げるとクロスドメインで弾かれる。

対応

Railsのサーバー側に変更を加える事で問題は解決できる。

① gemの導入

・rack-corsを追加

 gem 'rack-cors', :require => 'rack/cors'

② config/application を編集
・編集

config.middleware.use Rack::Cors do
 allow do
  origins '*'
  resource '*', :headers => :any, :methods => [:get, :post, :options]
 end
end

を追記。

上記を行うことでクロスドメインを受け付けることが可能となる。

19
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
20