ruby-on-rails - 如何使用 rack-cors 在 Access-Control-Expose-Headers 标头中声明 Content-Range
问题描述
为了从前端访问我的 api,它要求我在 Access-Control-Expose-Headers 标头中声明 Content-Range。我不知道具体怎么写。
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: ["Access-Control-Expose-Headers", "Content-Range: 0-24/319"],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
或者
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
expose: ["Content-Range: orders 0-24/319"],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
知道我写错了什么吗?
解决方案
根据原始 repo 中的测试用例key: value
,不可能通过 rack-cors 接受的路由定义中的 pair
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*',
headers: :any,
expose: ["Content-Range"],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
然后例如,您可以写入默认值,BaseController
然后从中继承每个控制器
class BaseController < ApplicationController
after_action :apply_content_range_header
protected
def apply_content_range_header
response.headers['Content-Range'] = 'orders 0-24/319'
end
end
然后在你的控制器中调用它
class ProductsController < BaseController
def index; end # your products#index will have Content-Range header
end
推荐阅读
- python - 如何加快我的 MySQL (InnoDB) 插入速度?
- pdf - PDF文件中较低索引字母的大小问题,例如:j,p,q,g等
- windows-installer - 更改项目设置 MSI 的产品代码和名称
- python - 替换列表 Python 中的所有元素
- ios - Swift 和 Xcode:如何构建和运行 iOS 应用程序的 GitHub 存储库?
- spring-boot - Spring Data CRUDRepository ClasscastException
- prolog - 比较列表的子列表
- asp.net - 实体框架中的列名无效
- amazon-web-services - IAM 角色未分配实例配置文件
- amazon-web-services - 更新 Fargate TaskDefinition 后,触发任务的 CloudWatch 事件因任务定义不活动而失败