使用Rack-cors和Grape添加自定义响应标头

我正在使用Ruby on Rails API开发一个Ionic(Cordova)应用程序。 我想在登录后使用响应头来返回令牌。 我正在使用rack-cors gem来制作Cross Origin Request:

application.rb中

 config.middleware.insert_after Rails::Rack::Logger, Rack::Cors, :logger => Rails.logger do allow do origins '*' resource '/api/*', :headers => :any, :methods => [:get, :post, :options, :put] end end 

和葡萄gem来管理我的API路线。 但是我找不到一种方法来为我的回复添加标题,因为我添加了rack-cors

我试过这个:

 header('Access-Token', user.token.key) 

但它不起作用。 无论我做什么,我最终得到这些标题:

{cache-control:“max-age = 0,private,must-revalidate”,content-type:“application / json”}

任何人都可以帮我解决这个问题吗?

我使用了gem 'devise_token_auth'

另外,我在application.rb中有这个配置。

  class Application < Rails::Application # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true config.middleware.use Rack::Cors do allow do origins '*' resource '*', :headers => :any, :expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'], :methods => [:get, :post, :options, :delete, :put] end end end