为什么不使用机架?

时间:2017-04-27 01:54:37

标签: ruby-on-rails ajax rack

配置/ application.rb中

require_relative 'boot'
require 'rails/all'
Bundler.require(*Rails.groups)

module MyApp
 class Application < Rails::Application
  config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :options, :patch, :delete]
      end
    end
  end
end

的Gemfile

gem 'rack-cors', :require => 'rack-cors'

another gems…

捆绑exec rake中间件

use Rack::Cors

another middleware…

这是控制台日志显示的错误

XMLHttpRequest cannot load https://example.com. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.

我该怎么办?

1 个答案:

答案 0 :(得分:-1)

尝试将代码更改为这样。我试过在我的轨道5只添加下面的行将工作。然后尝试重新启动服务器。

module YourApp
  class Application < Rails::Application
    # Rails 5

    config.middleware.insert_before 0, Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end
  end
end

我希望它有效。