reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

CORS error when trying to get google geocoding

Open youradds opened this issue 6 years ago • 4 comments

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

youradds avatar Jan 22 '19 14:01 youradds

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.

sapabg avatar Nov 29 '19 09:11 sapabg

@sapabg thanks I was occupying JWT.

urmanzaru avatar Mar 16 '20 19:03 urmanzaru

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

deepshah4855 avatar May 22 '21 15:05 deepshah4855

Cheers @sapabg sorted my issue.

samharvey44 avatar Apr 13 '22 10:04 samharvey44