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

joh*_*ser 6 javascript ruby-on-rails angularjs

所以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中,但这似乎没有任何影响.

我该如何解决这个错误?

小智 20

在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
Run Code Online (Sandbox Code Playgroud)

在config/routes.rb中:

match '*any' => 'application#options', :via => [:options]
Run Code Online (Sandbox Code Playgroud)

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

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