Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any interest in JSONP support? #28

Closed
arailsdemo opened this issue May 3, 2011 · 8 comments
Closed

Any interest in JSONP support? #28

arailsdemo opened this issue May 3, 2011 · 8 comments

Comments

@arailsdemo
Copy link

For example:

class MyAPI < Grape::API
  prefix 'api'
  version '1'

  callback 'my_callback'

  resources :posts do
    # GET /api/1/posts/:id
    get ":id" do
      Post.find(params[:id])
    end
  end
end

Then visiting http://0.0.0.0:3000/api/1/posts/1?b=c&my_callback=foo, would result in

foo({ ... });
@lenary
Copy link

lenary commented May 3, 2011

https://github.com/rack/rack-contrib/blob/master/lib/rack/contrib/jsonp.rb should probably do this for you to a certain extent, though you might not get any dsl'y goodness

@mbleigh
Copy link
Contributor

mbleigh commented May 26, 2011

You can do this now (albeit without a specified callback) just by doing as @lenary suggested:

class MyAPI < Grape::API
  use Rack::JSONP
end

As such I don't think this needs to be included in Grape proper.

@mbleigh mbleigh closed this as completed May 26, 2011
@apeckham
Copy link

apeckham commented Feb 7, 2012

I couldn't get this to work without reordering these two lines in api.rb:

middleware.each{|m| b.use *m }
b.use Grape::Middleware::Formatter, :default_format => default_format || :json

Rack::JSONP requires a Content-Type of application/json to do anything, but that's not set until the Formatter executes, so... I think middleware.each has to come before the Formatter.

A workaround is to wrap MyAPI in another Rack::Builder --

Rack::Builder.new {
   use Rack::JSONP
   run MyAPI
}

Holler if there's a better way!

@dblock
Copy link
Member

dblock commented Feb 20, 2012

If you mount Grape inside Rails, add the following into your config/application.rb instead.

module FooBar
  class Application < Rails::Application
    config.middleware.use Rack::JSONP
  end
end

Test with api/something?callback=parseResponse.

@volkanunsal
Copy link

I cannot get this to work in Rails.

@deckchairhq
Copy link
Contributor

Can you give some more information regarding how you have installed and initialised, including what error (if any) you've received?

@volkanunsal
Copy link

I'm not getting any error messages at all. The reason I know it's not working is the output of the endpoint is coming out as a plain old string, not as a serialized JSON file, and the output is not wrapped up by the function name.

I'm using Rails 3.2.11 on Ruby 1.9.3.

How else I can be helpful?

@deckchairhq
Copy link
Contributor

Could you create a new rails project, add grape to the gemfile and try and output JSON in the manner you are trying now. If it still breaks then it could very well be an issue. Otherwise it would point to an error in your code.

If you could try that and if its still broken push that project to a new github repo and we'll pull it down and take a look.

Cheers,

G

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants