0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Rails】CORSの設定方法

Posted at

rack-corsをインストール

Gemfileに以下を追記してbundle installします。

Gemfile
gem 'rack-cors'
bundle install

configを作成

config/initializers/cors.rbに設定を記述します。

config/initializers/cors.rb
# Be sure to restart your server when you modify this file.

# Avoid CORS issues when API is called from the frontend app.
# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests.

# Read more: https://github.com/cyu/rack-cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    # 許可するオリジンを記述(APIを叩く側のドメインを記述)。
    origins 'localhost:3000'

    # 許可するリソースファイルを記述
    resource '*',
      headers: :any,  # CORS リソース要求で許可される HTTP ヘッダー
      expose: ["access-token", "expiry", "token-type", "uid", "client"], # レスポンスの HTTP ヘッダをクライアントに公開
      methods: [:get, :post, :put, :patch, :delete, :options, :head],       # 許可するメソッドを記述
      
  end
end
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?