Skip to content

Instantly share code, notes, and snippets.

@rjungemann
Created June 21, 2010 01:21
Show Gist options
  • Save rjungemann/446278 to your computer and use it in GitHub Desktop.
Save rjungemann/446278 to your computer and use it in GitHub Desktop.
Example of using JSONP with Sinatra
# http://snippets.aktagon.com/snippets/445-How-to-create-a-JSONP-cross-domain-webservice-with-Sinatra-and-Ruby
require 'sinatra/base'
require 'json/pure'
module JSONPatra
class App < Sinatra::Base
get "/" do
callback = params['callback']
json = { :hello => "world!" }.to_json
content_type(callback ? :js : :json)
response = callback ? "#{callback}(#{json})" : json
response
end
end
end
run JSONPatra::App.new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment