Skip to content

Instantly share code, notes, and snippets.

@filipemonteiroth
Created November 24, 2016 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipemonteiroth/401fa7f28b0abd6c6c6bff860fd99a5b to your computer and use it in GitHub Desktop.
Save filipemonteiroth/401fa7f28b0abd6c6c6bff860fd99a5b to your computer and use it in GitHub Desktop.
CORS Rails
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
after_filter :cors
before_filter :cors_preflight_check
respond_to :json
private
def cors
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization, x-auth-token, responseType'
end
def cors_preflight_check
if request.method == 'OPTIONS'
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'Content-Type, X-Requested-With, X-Prototype-Version, x-auth-token, responseType'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment