SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I create an external API for a RoR app ?

    Hello,

    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

  2. #2
    Team ********* Louis Simoneau's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    228
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Louis,

    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

    Your URL:
    http://localhost:3000/api/get_items?callback=awesome

    In your api controller:
    Code:
    render :json => @items.to_json, :callback => params[:callback]
    Result:
    Code:
    awesome(["item1", item2"]);
    This gives you cross-domain JSONP. The callback variable in the URL is optional.

    There’s no need to roll your own render_json.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •