Reputation: 21
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:
Upvotes: 2
Views: 553
Reputation: 1935
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.
Upvotes: 2
Reputation: 3819
You're not hitting their endpoint. Since you didn't provide a URL scheme, it's being recognized as a relative path and you're hitting your localhost.
Upvotes: 0