You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React-admin对接Rails 6+ActiveStorage时CORS跨域错误求助(已配置S3及Rails CORS仍无效)

React-admin对接Rails 6+ActiveStorage时CORS跨域错误求助(已配置S3及Rails CORS仍无效)

各位好,我最近在做一个项目,用Rails 6搭配ActiveStorage把文件存在S3上,前端用react-admin,但是一上线就碰到了CORS跨域错误——本地跑的时候一切正常,线上却死活不行,实在搞不定来求助大家!

前端抛出的错误信息如下:

Access to fetch at 'https://my-backend.com' from origin 'http://my-react-admin-app.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

fetch.js:50 GET https://my-backend.com/data?_end=100&_order=ASC&_sort=id&_start=0&locale=en net::ERR_FAILED.

我已经尝试配置了S3桶的CORS策略,具体内容如下:

[
  {
    "AllowedHeaders": [
      "*"
    ],
    "AllowedMethods": [
      "HEAD",
      "GET",
      "PUT",
      "POST",
      "DELETE"
    ],
    "AllowedOrigins": [
      "*"
    ],
    "ExposeHeaders": [
      "X-Total-Count"
    ]
  }
]

同时也配置了Rails端的CORS,Gemfile里已经添加了相关依赖:

gem "rails", "~> 6.0.0"
gem "aws-sdk-s3", require: false
gem "rack-cors", "~> 1.0.3"

Rails的config/initializers/cors.rb配置内容如下:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins "*"
    resource "*",
      headers: :any,
      methods: [:get, :post, :put, :delete, :options],
      expose: "X-Total-Count"
  end
end

但做完这些配置后,CORS错误依然存在,有没有大佬能给点排查方向或者解决思路?

备注:内容来源于stack exchange,提问作者Mr Africa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 12:43:12