0
0

More than 1 year has passed since last update.

【Rails】rack-corsの設定方法

Posted at

はじめに

 本記事は、プログラミング初学者が、学習を進めていて疑問に思った点について調べた結果を備忘録も兼ねてまとめたものです。
 そのため、記事の内容に誤りが含まれている可能性があります。ご容赦ください。
 間違いを見つけた方は、お手数ですが、ご指摘いただけますと幸いです。

rack-corsの設定方法

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,
      # 許可するHTTPヘッダーを記述
      # ヘッダー情報を外部に公開
      expose: ["access-token", "expiry", "token-type", "uid", "client"],
      # 許可するメソッドを記述
      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