Coder Social home page Coder Social logo

Comments (8)

lenary avatar lenary commented on May 31, 2024

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

from grape.

mbleigh avatar mbleigh commented on May 31, 2024

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.

from grape.

apeckham avatar apeckham commented on May 31, 2024

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!

from grape.

dblock avatar dblock commented on May 31, 2024

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.

from grape.

volkanunsal avatar volkanunsal commented on May 31, 2024

I cannot get this to work in Rails.

from grape.

deckchairhq avatar deckchairhq commented on May 31, 2024

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

from grape.

volkanunsal avatar volkanunsal commented on May 31, 2024

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?

from grape.

deckchairhq avatar deckchairhq commented on May 31, 2024

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

from grape.

Related Issues (20)

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.