如何使用rack-cors在Access Control Expose-Headers标头中声明内容范围



为了从前端访问我的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

知道我写错了什么吗?

根据原始回购中的测试用例在路由定义内不可能通过key: value对将被机架cors 接受

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

最新更新