Sinatra, JavaScript Cross-Domain Requests JSON

后端 未结 4 1490
眼角桃花
眼角桃花 2021-02-06 19:58

I run a REST-API build on top of Sinatra. Now I want to write a jQuery Script that fetches data from the API.

Sinatra is told to response with JSON

befor         


        
4条回答
  •  深忆病人
    2021-02-06 20:05

    Thank's for the answers so far. You were right and jsonp would solve the problem. The code snippets for javascript work fine. To set up Sinatra is very easy as it is build on top of Rack. Therefore simply install the rack-contrib gem

     gem install rack-rack-contrib --source=http://gems.github.com/
    

    (or put it in your Gemfile) and add

    require 'rack/contrib/jsonp'
    use Rack::JSONP
    

    to your application.

    This middleware provides regular JSON to non-JSONP clients and JSONP to jQuery & co.

提交回复
热议问题