访问控制允许起源问题

问题描述:

我有两个本地Rails应用程序,我想互相交流以进行测试……一个运行在端口3000上,另一个运行在9292上.

I have two local rails apps that I would like to talk to each other for testing purposes... one is running on port 3000 and the other on 9292.

但是当我从localhost:3000到localhost:9292发出ajax请求时,我总是遇到此问题:

But when I make an ajax request from localhost:3000 to localhost:9292 I keep getting this issue:

Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.

关于如何解决此问题的任何想法?

Any idea on how to fix this?

我正在使用一个简单的Sinatra应用程序来接收(仅用于测试目的)JSON请求.下面是我如何在本地主机上互相交谈的两个Rails应用程序(一个在端口3000上,另一个在端口9292上)
工作代码

I am using a simple Sinatra app to receive (for testing purposes ONLY) JSON requests. Below is how I got two rails apps talking to each other on localhost (one on port 3000 and the other on port 9292)
Working Code

before do
  headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version, X-CSRF-Token'
end

after do
  headers['Access-Control-Allow-Origin'] = 'http://localhost:3000/'
end

希望这会有所帮助!

出于安全原因,通常不允许跨域AJAX.如果可以使用JSONP,则可以选择使用它.如果没有,您可以使用 flXHR 之类的方法来解决此限制.

Cross-domain AJAX is generally not allowed for security reasons. JSONP is an option if you are able to use it. If not, you can use something like flXHR to get around this restriction.

祝你好运!