ruby 返回jsonp

一: rails中返回jsonp


class BookController < ApplicationController

  layout false

  def show
    res = {:id => 10086, :name => 'CMCC'}
    render :json => res, :callback => params[:callback]
  end

end



二:sinatra中返回jsonp

首先执行:gem install sinatra-jsonp

然后在 Gemfile 里面添加 gem 'sinatra-jsonp'


require 'sinatra'
require 'sinatra/jsonp'

get '/hello' do
  data = ["hello","hi","hallo"]
  JSONP data, params[:callback]      # JSONP is an alias for jsonp method
end


get '/hi' do
  data = ["hello","hi","hallo"]
  jsonp data, params[:callback]
end


你可能感兴趣的:(ruby 返回jsonp)