访问控制允许起源问题 [英] Access-Control-Allow-Origin issues

查看:95
本文介绍了访问控制允许起源问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个本地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.

祝你好运!

这篇关于访问控制允许起源问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆