adi
Joined: 02/08/2024 18:48:37
Messages: 1
Offline
|
I need to get the location from coordinates in javascript, I'm not sure if I can use the 3geonames API that way, but I tried anyways and I got this error in console:
`https://api.3geonames.org/<the coordinates from my city>' from origin 'http://<some ip>' 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.`
this is my code:
let cachedAddress = null;
async function getAddress(coordenates) {
const lat = coordenates[0];
const lng = coordenates[1];
if (cachedAddress) {
return cachedAddress
} else {
try {
const data = await fetch(`https://api.3geonames.org/${lat},${lng}`, {method: 'GET', headers: {'Content-Type': 'application/json'}})
const dataJson = await data.json();
const city = dataJson.address.city;
const country = dataJson.address.country;
cachedAddress = [city, country];
return cachedAddress
} catch(error) {
console.log(error)
};
};
};
I'd appreciate any help.
|