19
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Rails(Grape)でAPIを作成する備忘録2

前回作ったAPIのクロスドメインアクセスを許可させてみた

やったこと

rack-corsを追加

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

config/applicationを編集

config/application.rb
module APISample
  class Application < Rails::Application
    ... 
    config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options, :patch, :delete]
      end
    end
    ...
  end
end

これだけで他ドメインからアクセスできるようになりました

参考
http://qiita.com/hkusu/items/b34f684b49751d9c9abd

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
19
Help us understand the problem. What are the problem?