Skip to content

Instantly share code, notes, and snippets.

@nbhartiya
Created February 19, 2015 22:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nbhartiya/067d6cad778ed7fded17 to your computer and use it in GitHub Desktop.
Save nbhartiya/067d6cad778ed7fded17 to your computer and use it in GitHub Desktop.
Javascript Embeddable Widget: Working with CORS
class YourController < ApplicationController
before_filter :cors_preflight_check, only: [:settings]
after_filter :cors_set_access_control_headers, only: [:settings]
def settings
# YOUR CODE TO GET CLIENT'S WIDGET SETTINGS HERE
end
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'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
end
@niyando
Copy link

niyando commented Jun 18, 2016

doesn't this alter headers for all requests to server once it hits your_controller#settings ?
Is there a way to limit this operation to just this action ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment