Created
June 16, 2015 17:18
-
-
Save jeffsaracco/97149cc311b7f2a131c1 to your computer and use it in GitHub Desktop.
Rails CORS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before_filter :cors_preflight_check | |
after_filter :cors_set_access_control_headers | |
def cors_set_access_control_headers | |
headers['Access-Control-Allow-Origin'] = '*' | |
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS' | |
headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token' | |
headers['Access-Control-Max-Age'] = "1728000" | |
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'] = 'X-Requested-With, X-Prototype-Version, Token, X-Blueprint-Token, Content-Type' | |
headers['Access-Control-Max-Age'] = '1728000' | |
render :text => '', :content_type => 'text/plain' | |
end | |
end | |
def handle_options_request | |
head(:ok) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
match '*path', controller: 'application', action: 'handle_options_request', via: [:options] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment