Rack middleware that turns all .jsonp requests into a jsonp response.
(does not support 'callback' since it is a really generic parameter name)
Btw, don't forget to give a try to J50Nπ (a pure JS JSONP helper), they make a lovely couple together :P
Roberto Decurnex (decurnex.roberto@gmail.com)
- Ryan Wilcox (rwilcox)
- Amiel Martin (amiel)
- Michael Grosser (grosser)
- Matt Sanford (mzsanford)
- joelmats (joelmats)
If you are using Bundler you can easily add the following line to your Gemfile:
gem 'rack-jsonp-middleware'
Or you can just install it as a ruby gem by running:
$ gem install rack-jsonp-middleware
In your config/application.rb
file add:
require 'rack/jsonp'
And, within the config block:
config.middleware.use Rack::JSONP
Here is an exelent example of this - Rails 3 Configuration Example
Thank you rwilcox!
Same as for Rails 3 but modifying the config/environment.rb
file instead.
In your config.ru
file add the following lines:
require 'rack/jsonp'
use Rack::JSONP
You can also clone the project with Git by running: $ git clone git://github.com/robertodecurnex/rack-jsonp-middleware
Given that http://domain.com/action.json returns:
{"key":"value"}
With the following Content-Type:
application/json
Then http://domain.com/action.jsonp?callback=J50Npi.success will return:
J50Npi.success({"key":"value"})
With the following Content-Type:
application/javascript
But http://domain.com/action.json?callback=J50Npi.sucess will still return:
{"key":"value"}
With the following Content-Type:
application/json
You can restrict the delivery of jsonp to a path prefix. For example, if
use Rack::JSONP, "/api/v1/"
is used, only requests to paths that start with /api/v1/ will answered with jsonp. JSONP requests to other paths will be answered with error code 400.
Supporting jsonp means that another websites can access your website on behalf of a user visiting their site, which might lead to security problems (e.g. they read http://yoursite.com/user.jsonp and get the users email etc), so think about if you want to turn it on globally.