【发布时间】:2016-02-14 20:10:28
【问题描述】:
错误:XMLHttpRequest 无法加载 https://thirdPartyasite/template.html。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'http://localhost:3000'。
我正在使用 Rails 应用程序,该应用程序通过 ajax 调用使用跨组织数据。此问题提供的答案不起作用。
1)How to set access-control-allow-origin in webrick under rails?
2)https://demisx.github.io/rails-api/2014/02/18/configure-accept-headers-cors.html
3)Allow anything through CORS Policy.
我无法控制客户端应用程序。如何克服这个问题。
我试过这个:
before_filter :set_headers
def set_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
end
还是会出现同样的错误。
也试过这个:
在 gem 文件中添加:gem 'rack-cors', :require => 'rack/cors'
在 config/application.rb 中:
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
仍然遇到同样的错误。
为开发环境工作: 安装 CORS Chrome 插件。 但我需要适当的解决方案
【问题讨论】:
-
您可能需要发布一些代码(到目前为止您所做的一切都不起作用)。
-
@LeonelMachavaI 用我的尝试更新了我的问题
标签: javascript ruby-on-rails angularjs ajax cors