I am working on react-app that uses google places API, my task is to get places in a selected location (location.lat
, location.lng
).
I created the next hook with the request URL:
useEffect(() => {
if (location.lat && location.lng) {
setUrl("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + location.lat + "," + location.lng + "&radius=2000" + <My_key> );
console.log("url = "+url);
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Access-Control-Allow-Origin": "*"
}
})
.then(response => response.json())
.then(result => setPlacesList(result));
console.log("places"+placesList);
}
}, [location.lat, location.lng, url, placesList]);
But I faced with the next error:
Access to fetch at 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=26.8466937,80.94616599999999&radius=2000&key=XXXX' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.