question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No 'Access-Control-Allow-Origin' header is present on the requested resource

See original GitHub issue

I’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:

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:7
  • Comments:23 (3 by maintainers)

github_iconTop GitHub Comments

28reactions
bparanjcommented, May 14, 2017

This works:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

This does not work:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'http://localhost:3000'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

This also does not work:

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'localhost:3000', '127.0.0.1:3000'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

Adding:

  match '*path', via: [:options], to: lambda {|_| [204, { 'Content-Type' => 'text/plain' }]}

to routes does not help.

8reactions
asuxcommented, Aug 9, 2016

Also tried all solutions and gem simply not works!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reason: CORS header 'Access-Control-Allow-Origin' missing
The response to the CORS request is missing the required Access-Control-Allow-Origin header, which is used to determine whether or not the ...
Read more >
Why does my JavaScript code receive a "No 'Access-Control ...
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. I know that the API ...
Read more >
Fixing "No 'Access-Control-Allow-Origin' Header Present"
This error occurs when a script on your website/web app attempts to make a request to a resource that isn't configured to accept...
Read more >
3 Ways to Fix the CORS Error — and How the Access-Control ...
This header contains an Access-Control-Allow-Origin key, to specify which origins can access the server's resources. The key will have one ...
Read more >
Resolve the "No 'Access-Control-Allow-Origin' header" error ...
Resolution. Confirm the origin's cross-origin resource sharing (CORS) policy allows the origin to return the Access-Control-Allow-Origin header.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found