【问题标题】:CORS issue with upgrade Rails 4 to Rails 5将 Rails 4 升级到 Rails 5 时出现 CORS 问题
【发布时间】:2017-09-27 15:47:19
【问题描述】:

我正在将 Rails 应用程序从 Rails 4.1.1 升级到 Rails 5.1.4。 我有 2 个应用程序,一个是 Web 应用程序(用于 angular),另一个是 API 应用程序。

我在做什么,我从Web app(Rails with Angular) 发送请求并从API app 获取数据。但是当我从Web app 发送请求时,出现以下错误:

无法加载http://api.myapp:3001/user:对预检的响应 请求未通过访问控制检查: 响应中的“Access-Control-Allow-Origin”标头不能是 当请求的凭据模式为“包含”时,通配符“*”。起源 'http://myapp:3000' 因此不允许访问。凭据 XMLHttpRequest 发起的请求模式由 withCredentials 属性。

http://myapp:3000' -> Web application and request sending to API

http://api.myapp:3001/user -> API application to send response

google 问题之后,我发现了一个名为rack-cors 的宝石。 在 Web 和 API 这两个应用程序中,我都添加了

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

但仍然遇到问题。请告诉我。

【问题讨论】:

标签: ruby-on-rails cors ruby-on-rails-5 rack-cors


【解决方案1】:

好吧,消息:

当请求的凭据模式为“包含”时,响应中的“Access-Control-Allow-Origin”标头的值不能是通配符“*”]

您似乎正在从 Angular 应用程序发送凭据信息,并且该域未列入白名单。为了测试,您可以尝试将 withCredentials 设置为 false,或允许该域。

这可以帮助你:http://50linesofco.de/post/2017-03-06-cors-a-guided-tour

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2013-07-24
    • 1970-01-01
    相关资源
    最近更新 更多