Skip to content

Instantly share code, notes, and snippets.

@jccarbonfive
Created February 27, 2012 04:57
Show Gist options
  • Select an option

  • Save jccarbonfive/1921504 to your computer and use it in GitHub Desktop.

Select an option

Save jccarbonfive/1921504 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
function parseUser (json) {
var user = new User(json)
// do something with the user
}
</script>
<script type="text/javascript"
src="http://server2.example.com/users/1.js?callback=parseUser">
</script>
gem 'rack-cors',
:require => 'rack/cors'
module Sample
class Application < Rails::Application
# other application config
config.middleware.use Rack::Cors do
allow do
origins 'server1.example.com'
resource %r{/users/\d+.json},
:headers => ['Origin', 'Accept', 'Content-Type'],
:methods => [:put, :delete]
end
end
end
end
class UsersController < ApplicationController
def show
respond_to do |format|
format.js do
@user = User.find params[:id]
render :json => @user, :callback => params[:callback]
end
end
end
end
$ curl http://localhost:3000/users/1.js\?callback\=parseUser
parseUser({"age":50,"created_at":"2012-02-22T05:44:28Z","id":1,"name":"user #1","updated_at":"2012-02-22T05:44:28Z"})
gem 'rack-jsonp-middleware',
:require => 'rack/jsonp'
module Sample
class Application < Rails::Application
# other application config
config.middleware.use Rack::JSONP
end
end
<script type="text/javascript"
src="http://server2.example.com/users/1.jsonp?callback=parseUser">
</script>
class UsersController < ApplicationController
respond_to :json
def show
@user = User.find params[:id]
respond_with @user
end
end
$ curl http://localhost:3000/users/1.jsonp\?callback\=parseUser
parseUser({"age":50,"created_at":"2012-02-22T05:44:28Z","id":1,"name":"user #1","updated_at":"2012-02-22T05:44:28Z"});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment