I'm trying to code a weather app that displays temperature according to your current location ,using OpenWeatherApi, I get an error 404 I don't know why
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
long = position.coords.longitude;
lat = position.coords.latitude;
console.log(position);
const api = `api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${long}&appid=74913cd6ec927a299e7fd99534e17b5f`;
fetch(api)
.then(response => {
return response.json();
})
.then(data => {
console.log(data);
});
});
The error I'm getting in the console:

You need to prepend https:// to your URL. For example,
❌ api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API key}
✅ https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API key}
The API call on the OpenWeather occasionally excludes https:// from their API call URLs on their website. However when making an API call in your JS code, you cannot omit this because otherwise your code will convert your URL to a relative path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With