Skip to content

Force the content type to text/javascript for JSONP requests #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/controllers/scripts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class ScriptsController < ApplicationController
respond_to :html, :json, :xml
before_filter :load_script, only: [:view, :destroy]
skip_before_action :verify_authenticity_token, if: :json_request?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without it Rails refuses to send the data back with the text/javascript content type which is required by JSONP to work


def create

Expand Down Expand Up @@ -40,7 +41,13 @@ def view
response.headers.except! 'X-Frame-Options'

respond_with @script do |format|
format.json { render :json => @script.to_json, :callback => params[:callback] }
format.json {
if params[:callback]
render :json => @script.to_json, :callback => params[:callback], :content_type => 'text/javascript'
else
render :json => @script.to_json
end
}
end
end

Expand Down Expand Up @@ -74,6 +81,10 @@ def destroy

private

def json_request?
request.format.json?
end

def load_script
@script = Script.find_by_slug(params[:slug])
end
Expand Down