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']
Written by Jay Aniceto
Related protips
2 Responses
Thanks
over 1 year ago
·
Thanks
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#