Last Updated: July 25, 2019
·
12.06K
· 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']

2 Responses
Add your response

Thanks

over 1 year ago ·

Thanks

over 1 year ago ·