Ruby on rails 为什么只有自定义Desive API调用的选项会出现404错误?
我一直在努力让我的API与CORS一起工作。我按照以下指示行事: 将此添加到my application.rb:Ruby on rails 为什么只有自定义Desive API调用的选项会出现404错误?,ruby-on-rails,devise,cors,Ruby On Rails,Devise,Cors,我一直在努力让我的API与CORS一起工作。我按照以下指示行事: 将此添加到my 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, re
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 Desive调用之外,我的所有调用都可以工作。当我在Chrome开发工具中查看时,我的Desive 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
...
关于为什么我的自定义Desive API调用不起作用而我的其他调用却起作用,你有什么想法吗
谢谢 成功了。只是切换到rack cors gem,而不是上面的代码,并遵循博客示例: