Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change the JSON renderer to enforce the 'JS' Content Type

The controller can set the response format as 'JSON' before the renderer code be
evaluated, so we must replace it when necessary.

Fixes #15081
  • Loading branch information...
commit b00b638b95dc513f4ba854ba3a96b7a8f344e4cc 1 parent a410427
@lucasmazza lucasmazza authored
View
7 actionpack/CHANGELOG.md
@@ -1,3 +1,10 @@
+* JSONP responses are now rendered with the `text/javascript` content type
+ when rendering through a `respond_to` block.
+
+ Fixes #15081.
+
+ *Lucas Mazza*
+
* Add `config.action_controller.always_permitted_parameters` to configure which
parameters are permitted globally. The default value of this configuration is
`['controller', 'action']`.
View
5 actionpack/lib/action_controller/metal/renderers.rb
@@ -112,7 +112,10 @@ module All
json = json.to_json(options) unless json.kind_of?(String)
if options[:callback].present?
- self.content_type ||= Mime::JS
+ if self.content_type.nil? || self.content_type == Mime::JSON
+ self.content_type = Mime::JS
+ end
+
"#{options[:callback]}(#{json})"
else
self.content_type ||= Mime::JSON
View
13 actionpack/test/controller/mime/respond_to_test.rb
@@ -128,6 +128,12 @@ def all_types_with_layout
end
end
+ def json_with_callback
+ respond_to do |type|
+ type.json { render :json => 'JS', :callback => 'alert' }
+ end
+ end
+
def iphone_with_html_response_type
request.format = :iphone if request.env["HTTP_ACCEPT"] == "text/iphone"
@@ -511,6 +517,13 @@ def test_html_type_with_layout
assert_equal '<html><div id="html">HTML for all_types_with_layout</div></html>', @response.body
end
+ def test_json_with_callback_sets_javascript_content_type
+ @request.accept = 'application/json'
+ get :json_with_callback
+ assert_equal 'alert(JS)', @response.body
+ assert_equal 'text/javascript', @response.content_type
+ end
+
def test_xhr
xhr :get, :js_or_html
assert_equal 'JS', @response.body
Please sign in to comment.
Something went wrong with that request. Please try again.