xew0cw
Last Updated: September 09, 2018
·
10.92K
· jayncoke

Render a jsonp response in Rails

If you need to render a <a href="http://en.wikipedia.org/wiki/JSONP">jsonp</a> response to allow for cross-domain capabilities, you don't have to go out of your way to build it. Rails has a way for you to do it already!

The <a href="http://railsapi.com/doc/rails-v2.3.8/classes/ActionController/Base.html#M001802">render</a> method has an option to build a callback around the json response called 'callback'. Pass a string to it, and it will wrap your json response with 'callback()' that is needed for your jsonp standardized response.

render :json => response.to_json, :callback => params['callback']

3 Responses
Add your response

12918

Thanks

over 1 year ago ·
18299

Thanks

over 1 year ago ·
30656