0
0

【Rails API】rack-corsでレスポンスヘッダーにアクセスできるようにする方法

Last updated at Posted at 2024-03-26

rack-corsをインストールしてconfig/initializers/cors.rbexposeを記述することでJavaScriptからレスポンスヘッダーにアクセスできるようになります。

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'localhost:3000'

    resource "*",
             headers: :any,
             methods: %i[get post put patch delete options head],
             expose: %w[X-Sample],
             credentials: true
  end
end

上記の場合にはX-Sampleにアクセス可能になります。

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