Created
February 27, 2012 04:57
-
-
Save jccarbonfive/1921504 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem 'rack-cors', | |
| :require => 'rack/cors' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ 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"}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem 'rack-jsonp-middleware', | |
| :require => 'rack/jsonp' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Sample | |
| class Application < Rails::Application | |
| # other application config | |
| config.middleware.use Rack::JSONP | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script type="text/javascript" | |
| src="http://server2.example.com/users/1.jsonp?callback=parseUser"> | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UsersController < ApplicationController | |
| respond_to :json | |
| def show | |
| @user = User.find params[:id] | |
| respond_with @user | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ 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