reqwest
reqwest copied to clipboard
CORS error when trying to get google geocoding
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
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 thanks I was occupying JWT.
Thanks @sapabg I had the same issue and your answered helped me to resolve it.
Cheers @sapabg sorted my issue.