xew0cw
Last Updated: February 25, 2016
·
10.53K
· 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']
Say Thanks
Respond

2 Responses
Add your response

12918
98a89aadebf8762f7394a7afbee2fba2

Thanks

over 1 year ago ·
18299
40d66815f031a2ba6d62c40786895d0f

Thanks

over 1 year ago ·