Internetbureau Holder

JSONP

Jeroen Bulters wo 12 okt 11

Unfortunately, by default there is no JSONP mime type defined in Railsusage of the JSONP mime type defined in the documentation of Rails. So you’re left a bit in the void on that part. I thought Rails was a one stop solution.

Luckily, the solution is easy:

class ApplicationController < ActionController::Base
  protected
  def render_json(json, options={})
    callback, variable = params[:callback], params[:variable]
    response = if callback && variable
                   "var #{variable} = #{json};\n#{callback}(#{variable});"
                 elsif variable
                   "var #{variable} = #{json};"
                 elsif callback
                   "#{callback}(#{json});"
                 else
                   json
                 end
               end
    render({:content_t ype => :js, :text => response}.merge(options))
  end
end

And you’re able to render your jsonp replies by calling:

format.js {
    # do your thang 
    render_json result.to_json
}

Still, it’s a shame you have to hack this in yourself.

As Jan de Poorter rightfully noted: The functionality is available; just poorly documented. My lesson here is to check the Rails source more often.

Gepost in hor |  2 reacties

Jeroen Bulters do 13 okt 11 09:15

You are correct and I have adjusted the post above accordingly. Thanks for pointing me in the right direction.

(sidenote: I left the post almost intact, just for the record).

Jan De Poorter wo 12 okt 11 17:45

Actually, you can do

render :json => object, :callback => params[:callback]

And it works. It’s not documented in the default Rails documentation, though.

Here’s the implementation code:
https://github.com/rails/rails/blob/4f044528c0373b0a581a12b1311a7a544ded6c58/actionpack/lib/action_controller/metal/renderers.rb#L96

Plaats je reactie





Welcome to Holland On Rails

This weblog is the official Ruby techblog from the guys at Holder, a Ruby development company. Holder is also the company behind the RubyAndRails Europe Conference in Amsterdam.

Recente Jobs


Bekijk alle jobs »»

Gereedschapskist

Onmisbare tools voor
iedere developer!
Ruby On Rails
Framework voor de web 2.0 developer. Eindelijk vooruitgang!
TextMate
Editor for true pro's
Typ, tab, top :-)
Nee, niet voor Win.
Made On A Mac
En nou is het over met die saaie grijze Windows bak van je!

Auteurs op deze site

Chris Obdam

'Less is more' evangelist, past dit ook dagelijks toe op zijn tandenborstel.

Chiel Wester

Snelheidswonder op Ruby wielen. Leuk om mee te pair-programmen ;-) Recommend Me

Stephan Kaag

Het eerste Rails coreteam- member uit Nederland? Rails evangelist van het eerste uur.

Paul Engel

Én Rails programmeren én interfaces designen? Je zou hem superman kunnen noemen..

Dax Huiberts

Official Zip-Programmer, skinny code is helemaal zijn ding. Haalt meer code weg dan dat er bij komt.

Freek Monteban

Het nieuwste telg uit het Holland on Rails nest! Hij doet niets anders meer!

Johan Vermeulen

De stylesheet-koning uit de kop van Noord-Holland!