当我尝试从前端登录用户时,在浏览器控制台中出现以下错误:
Access to XMLHttpRequest at 'http://localhost:3000/signin' from origin 'http://192.168.178.202' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我的后端是在Puma上运行的Rails API。
这是我的Puma配置:
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count, threads_count
app_dir = File.expand_path("../..", __FILE__)
shared_dir = "#{app_dir}/shared"
bind "unix://#{shared_dir}/sockets/puma.sock"
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
pidfile "#{shared_dir}/pids/puma.pid"
state_path "#{shared_dir}/pids/puma.state"
activate_control_app
port ENV.fetch("PORT") { 3000 }
environment ENV.fetch("RAILS_ENV") { "production" }
workers ENV.fetch("WEB_CONCURRENCY") { 4 }
preload_app!
on_worker_boot do
require "active_record"
ActiveRecord::Base.connection.disconnect! rescue ActiveRecord::ConnectionNotEstablished
ActiveRecord::Base.establish_connection(YAML.load_file("#{app_dir}/config/database.yml")[rails_env])
end
# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
这是我的CORS配置-为了进行测试,我现在允许所有起源:
Rails.application.config.middleware.insert_before 0, Rack::Cors, debug: true, logger: (-> { Rails.logger }) do
allow do
origins '*'
resource '*',
headers: :any,
credentials: false,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
有人可以解释一下,为什么我仍然在上面提到的控制台中收到此错误?
谢谢!
更新:
我将前端更改为使用http://192.168.178.202:3000
而不是http://localhost:3000
,但似乎并没有帮助,因为端口不同。还有其他想法吗?这是我得到的错误的屏幕截图: