用于JSONP请求的Rails mime类型标头

时间:2013-12-13 17:44:22

标签: ruby-on-rails jsonp mime-types

我正在努力获取JSONP请求以“application / javascript”类型而不是“application / json”返回。

来源:

class ApplicationController < ActionController::Base
  respond_to :json
...

class WeatherController < ApplicationController
  def forecast
    if params[:lat].blank? || params[:lng].blank?
      respond_with({ :error => 'Must provide lat and lng' }, :status => :bad_request,
                :location => :nil)
    end

    forecast = WeatherService.forecast_io(params[:lat], params[:lng], params[:time])
    if forecast != nil
      respond_with(forecast, { :location => nil, :callback => params[:callback] })
    else
      respond_with({ :error => 'No weather data' }, {
        :status => :not_found,
        :callback => params[:callback]
      })
    end
  end

当尝试使用respond_to :js并强制执行javascript格式测试时,我得到:

网址: http://localhost:3000/forecast/42.2214941/-83.4638431?callback=jQuery20301419312770012766_1386953327718&_=1386953327719 "Unknown format" error

0 个答案:

没有答案