所以StackOverflow上有一些问题可以解决这个错误,但在我检查过的10-15个问题中,我无法找到解决我的确切问题的方法.
我在远程服务器上运行Angular应用程序(端口9000)和Rails应用程序(端口3000).角度应用程序通过发布请求向rails应用程序发送请求.
发出请求时,Javascript控制台会显示以下错误消息:
XMLHttpRequest cannot load http://0.0.0.0:3000/api/query. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.241.16.159:9000' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
从我读过的内容来看,我需要更改一下我的Rails应用程序,以便它可以接受来自其他服务器的连接(这看起来很奇怪,因为两个应用程序都运行在同一个ec2实例上).
我试过添加一行像
skip_before_filter :verify_authenticity_token
Run Code Online (Sandbox Code Playgroud)
我的控制器在rails中,但这似乎没有任何影响.
我该如何解决这个错误?
我在Rails5上,我想在我的一条路线上允许CORS.以下是我可以在所有路由中使用CORS的方法,但有没有办法只为一个端点列入白名单?
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
Run Code Online (Sandbox Code Playgroud)