Giter Site home page Giter Site logo

restful_jsonp's Introduction

RESTful JSONP

This is a Rails plugin (Railtie) that allows your existing RESTful controllers to work over JSONP.

POST/PUT/DELETE

REST is designed around the four HTTP request methods (GET, POST, PUT, and DELETE), but JSONP requests are always GETs. To get around this restriction, this Railtie lets you specify the desired method in a special _method parameter.

For example, to make a PUT request to /users/1.json, you would make a JSONP (GET) request to /users/1.json?_method=PUT.

Responder

The plugin also adds a custom Responder for JSONP that automatically adds special behaviour to respond_with (via a custom display method).

For JSONP requests handled with respond_with:

  1. :callback => param[:callback] will be automatically added to the render options
  2. Error :status codes (e.g. :unprocessable_entity) will be changed to :accepted, since error responses cannot be handled by JSONP. To detect errors in JSONP responses check for an error attribute in the returned data.

Installation

First, install the gem:

gem install restful_jsonp

Then add this to your Rails app's Gemfile:

`gem 'restful_jsonp'

Note that this only works for Rails 3. In principle you could try to swap in the RestfulJSONP::MethodOverride middleware into a Rails 2.3+ app, but this has not been tested.

To enable the custom JSONP Responder, add this to your ApplicationController (or any controller that you want to enable the responder for):

self.responder = RestfulJSONP::JSONPResponder

How it Works

The _method functionality is built in to Rails (via Rack), but is normally only available for POST requests. This Railtie replaces the default Rack::MethodOverride middleware with a slightly altered version that checks for the _method parameter regardless of whether it's in a POST or GET request.

Note that this functionality is enabled for all requests, regardless of whether they are done via JSONP or otherwise.

History

1.0.2
  • Fixed error response processing in JSONPResponder
1.0.0
  • Initial release

restful_jsonp's People

Contributors

zuk avatar

Stargazers

Maxime Bedard avatar Robin Wunderlin avatar Andy Pham avatar James avatar Homan Chou avatar Jeshua Irving avatar Corey Reece avatar

Watchers

 avatar Hiroki Terashima avatar Anthony Perritano avatar Armin avatar  avatar Mike T. avatar gbby avatar Nate Titterton avatar James Cloos avatar Michael Moncada avatar Antonio Gamba-Bari avatar Geoffrey Kwan avatar Peter Malcolm avatar  avatar Ole Smørdal avatar Tom Berry avatar Gaurav Gupta avatar

restful_jsonp's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.