No 'Access-Control-Allow-Origin' header is present on the requested resource
See original GitHub issueI’m trying to implement google places api in my rails app for finding restaurants near my location.
This is the error I’m getting when trying to use the function that is fetching the data from the ajax request:
XMLHttpRequest cannot load . Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
I’ve added this snippet into my config/application.rb file:
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
This is the ajax request from my view (coffeescript):
$.ajax
url: url
dataType: "json"
type: "GET"
and I’ve noticed that my request headers remain unchanged.
(Access-Control-Allow-Origin is not added to request headers)
Request URL: (not relevant)
Request Method:OPTIONS
Status Code:200
Remote Address: (not relevant)
Response Headers
alt-svc:quic=":443"; ma=2592000; v="33,32,31,30,29,28,27,26,25"
alternate-protocol:443:quic
cache-control:public, max-age=300
content-encoding:gzip
content-length:538
content-type:application/json; charset=UTF-8
date:Mon, 16 May 2016 12:59:48 GMT
expires:Mon, 16 May 2016 13:04:48 GMT
server:pablo
status:200
vary:Accept-Language
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
Request Headers
Provisional headers are shown
Access-Control-Request-Headers:accept, x-csrf-token
Access-Control-Request-Method:GET
Origin:http://localhost:3000
Referer:http://localhost:3000/restil/new?
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
Query String Parameters
view source
view URL encoded
location:( not relevant)
radius:5000
type:restraunt
key: (not relevant)
jsonp is not an option because it’s not supported by places api
Tried everything that’s written in these threads:
- https://github.com/cyu/rack-cors/issues/33
- http://stackoverflow.com/questions/18538549/cant-get-rack-cors-working-in-rails-application (config.middleware.insert 0, Rack::Cors do)
- https://demisx.github.io/rails-api/2014/02/18/configure-accept-headers-cors.html
- http://stackoverflow.com/questions/36406869/add-custom-response-header-with-rack-cors-and-grape (using :expose key)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:7
- Comments:23 (3 by maintainers)
Top GitHub Comments
This works:
This does not work:
This also does not work:
Adding:
to routes does not help.
Also tried all solutions and gem simply not works!