ded / reqwest

browser asynchronous http requests
2.94k stars 341 forks source link

CORS error when trying to get google geocoding #273

Open youradds opened 5 years ago

youradds commented 5 years ago

Hi,

I'm trying to create a function on my site that will enable a user to click a button, and then search based on their location. I'm trying to use Googles geocoder feature:

                navigator.geolocation.getCurrentPosition(function(position) {

                    reqwest({
                        url: 'https://maps.googleapis.com/maps/api/geocode/json',
                        type: 'json',
                        method: 'post',
                        data: { latlng: position.coords.latitude + "," + position.coords.longitude, key: 'xxxx' },
                        error: function (err) { },
                        success: function (data) {

                            console.dir(data)

                        }
                    });

                });

This tries to run, but I get an error thrown back:

Access to XMLHttpRequest at 'https://maps.googleapis.com/maps/api/geocode/json' from origin 'https://m.mysite.org' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

The headers are correct as far as I can see:

https://ibb.co/pxNGGKd

Any ideas what to try next?

Thanks

Andy

sapabg commented 5 years ago

Make sure you are not sending any additional headers with your request. I had the same problem because of my JwtInterceptor adding 'Authorisation' header to google requests.

urmanzaru commented 4 years ago

@sapabg thanks I was occupying JWT.

deepshah4855 commented 3 years ago

Thanks @sapabg I had the same issue and your answered helped me to resolve it.

samharvey44 commented 2 years ago

Cheers @sapabg sorted my issue.