smallcog — 2011-01-24T17:06:54-05:00 — #1
I have created a simple Ruby on Rails app I use for storing a list of contacts. What I'd like is to allow other websites to include this contact data in their websites. This seems to g ive problems with same-domain only restrictions which aim to restrict cross-site scripting.
Any ideas on how I can get around this ?
Best regards / Colm
louis_simoneau — 2011-02-02T21:24:08-05:00 — #2
If you'd like your API to be available client-side on other domains, JSONP is probably your best bet (http://en.wikipedia.org/wiki/JSON#JSONP)
In Rails, that's as simple as calling render :json with a :callback parameter, as detailed here: http://kevinchiu.org/archives/jsonp-in-rails-3
smallcog — 2011-02-03T04:29:27-05:00 — #3
Thats exactly the solution I need, now working a treat with a little jquery on one end and the kevinchiu rails bit.
Thanks for that
Here's the kevinbiu advice
In your api controller:
render :json => @items.to_json, :callback => params[:callback]
This gives you cross-domain JSONP. The callback variable in the URL is optional.
There’s no need to roll your own render_json.