配置/ 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耙中間件
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.
我該怎麼辦?
你使用Rails 5或Rails 4嗎? – akbarbin
@MuhamadAkbarBinWidayat我使用Rails 5! – faara
再次閱讀錯誤消息。 'XMLHttpRequest無法加載https:// example.com'。如果您正在從https:// example.com加載數據,則無論您在本地主機上發送多少個cors頭文件都無關緊要。 ;) – max