XMLHttpRequest请求的资源上没有“Access-Control-Allow-Origin”标头

所以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. 

从我读过的内容来看,我需要更改一下我的Rails应用程序,以便它可以接受来自其他服务器的连接(这看起来很奇怪,因为这两个应用程序都运行在同一个ec2实例上)。

我试过添加一行像

  skip_before_filter :verify_authenticity_token 

我的控制器在rails中,但这似乎没有任何影响。

我该如何解决这个错误?

在app / controllers / application_controller.rb中:

 before_filter :add_allow_credentials_headers def add_allow_credentials_headers # https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#section_5 # # Because we want our front-end to send cookies to allow the API to be authenticated # (using 'withCredentials' in the XMLHttpRequest), we need to add some headers so # the browser will not reject the response response.headers['Access-Control-Allow-Origin'] = request.headers['Origin'] || '*' response.headers['Access-Control-Allow-Credentials'] = 'true' end def options head :status => 200, :'Access-Control-Allow-Headers' => 'accept, content-type' end 

在config / routes.rb中:

 match '*any' => 'application#options', :via => [:options] 

注意,这是响应OPTIONS请求,如果您的Angular前端正在执行POST请求,您将需要这些请求。 我使用Access-Control-Allow-Credentials进行的操作是针对我的应用程序的,因此cookie是从前端发送的。

我强烈建议您在上面的代码中阅读该mozilla链接中的文档。 它对CORS有一个非常彻底的解释。 您可能会发现上面的代码对您的目的来说过于宽松。

如果您还没有读过它,请告诉我一些问题是什么……

您的应用的CORS政策存在问题

CORS(Corss Origin资源共享)

当您使用XHR( XML HTTP REQUEST )访问另一台服务器上的endpoint时,您的应用程序(尽管我不确定如何)将默认拒绝访问请求资源。

发生这种情况的原因是为了确保只通过XHR提供某些数据/资源,这就是为什么它主要用于API之类的原因。

在此处输入图像描述

机架CORS

无论如何,你需要允许访问你的Rails应用程序中的Java应用程序 – 我个人会推荐使用rack-CORS gem:

 #Gemfile gem 'rack-cors' #config/application.rb config.middleware.use Rack::Cors do allow do origins '*' resource 'api/jquery', :headers => :any, :methods => [:get, :post] end end 

这将“允许”来自您不同应用程序的请求,为您解决问题。