1

我一直在努力让我的 API 与 CORS 一起工作。我按照以下说明操作:http: //kellishaver.tumblr.com/post/40758797489/cors-headers-with-devise

将此添加到我的 application.rb 中:

config.middleware.use "Cors"
config.middleware.insert_before Warden::Manager, "Cors"

将此添加到 /lib/cors.rb:

class Cors
  def initialize(app)
  @app = app
end

def call(env)
   status, headers, response = @app.call(env)

   headers['Access-Control-Allow-Origin'] = '*'
   headers['Access-Control-Allow-Methods'] = 'DELETE, GET, HEAD, OPTIONS, POST, PUT'
   headers['Access-Control-Max-Age'] = "1728001"

   [status, headers, response]
 end
end

当我试图解决同样的问题并允许从 Chrome(或其他任何地方)调用我的 API 时。问题是除了我的自定义 API 设计调用之外,我的所有调用都有效。当我在 Chrome 开发工具中查看“OPTIONS”时,我的 Devise API 调用会出现奇怪的 404 错误。

我的设计 API 看起来像这样......

class Api::V1::SessionsController < Devise::SessionsController
   skip_before_filter :verify_authenticity_token,
                 :if => Proc.new { |c| c.request.format == 'application/json' }

   respond_to :json
   def create
   warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")

   render :status => 200,
       :json => { :success => true,
                  :info => "Logged in",
                  :data => { :auth_token => current_user.authentication_token },
                  :id => current_user.id}
  end
...

关于为什么我的自定义设计 API 调用不起作用但我的其他调用不起作用的任何想法?

谢谢!

4

1 回答 1

0

得到它的工作。刚刚切换到 rack-cors gem 而不是上面的代码,并按照这里的博客示例: https ://gist.github.com/AdrianTeh/3561310

于 2013-07-07T03:18:48.207 回答