Created
July 19, 2014 07:11
-
-
Save serv/441cb713bb55c6fdeb80 to your computer and use it in GitHub Desktop.
Example of setting header to support CORS on Rails 4 API server side
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
module Api | |
module V0 | |
class LinksController < ApplicationController | |
# THIS | |
before_action :set_headers | |
def show | |
@link = Link.find(params[:id]) | |
render json: @link | |
end | |
private | |
def link_params | |
params.require(:status).permit(:title, :href, :comment) | |
end | |
# THIS | |
def set_headers | |
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' | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment