Closed mongeta closed 4 years ago
@mongeta My guess is that you have another instance of Rack::Cors
somewhere. I'm not sure how that would cause these issues, but I would try to clear that up first. Perhaps it is also defined in config.ru
or application.rb
?
Ok, I'll try to have a look, it was a very old legacy app, thanks for the idea.
Hi, I'm developing app with Rails and Nuxt.js. I faced the same error so here my solutions:
Rails 6.0.0.2
Nuxt.js
I wrote this code in Nuxt.js. ($axios posts and gets response from rails server if successfully did)
async login(vuexContext, data) {
await this.$axios.post("/api/v1/auth/sign_in", data)
.then( response => {
console.log(response.headers)
})
.catch(e => {
console.log(e)
})
}
my rack-cors settings below(in config/application.rb
):
Rails.application.config.middleware.use Rack::Cors do
allow do
origins 'localhost:3001' #my nuxt app on localhost:3001
resource '*',
headers: :any,
expose: ['access-token', 'client', 'expiry'],
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end
with above settings, my result of console.log(response.headers)
is:
headers:
cache-control: "max-age=0, private, must-revalidate"
content-type: "application/json; charset=utf-8"
I googled this trouble and I got @cyu 's reply below: https://github.com/cyu/rack-cors/issues/185#issuecomment-555842186
I checked my files with git grep Rack
,
and I found the same setting in config/initializers/cors.rb
!
So I moved this setting to config/initializers/cors.rb
, and works!!! :-D
here's my result:
headers:
access-token: "VPPrzn2DnJuqJi_uDEI4hg"
cache-control: "max-age=0, private, must-revalidate"
client: "TVmLwY1GM23koi9r9HY17g"
content-type: "application/json; charset=utf-8"
expiry: "1587634926"
Closing this issue. @raimon-kantox If you're still having this issue, please feel free to re-open (with more information of course).
Given those details:
ruby 2.4.0 rails: 5.2.1 rack-cors: 1.0.2 and 1.0.3
active_model_serializers: 0.10.7 api-pagination: 4.8.2 pagy: 3.2.1
config/initializers/cors.rb
The headers for expose are blank.
What I see is two calls to
use Rack::Cors
. Debugging the gem, I found that when the self.expos is informed, the first time that it does, it fills correct, but at the same time is called, it's empty.The first iteration is informed, I can see the expose details, but at the next iteration, they are gone. I say two iterations from the same connection, there is only ONE connection, but two iterations.
What can be the error of the two consequent calls of it?
But, if I use this code, the expose headers are exposed, but with other problems:
This is what I get in Chrome Network Inspector
The debug mode adds de X-Rack-CORS-...... and I can see there my expose headers. Even the allow-methods are different