Need help with the local weather project, OpenWeatherMap api not working

Need help with the local weather project, OpenWeatherMap api not working
0.0 0

#1

I was working on the weather project. When I tried to get weather information from OpenWeatherMap, the API call was not responding. I searched for the forums. It said that was the http/https issue. Still, I have no idea how to fix it.
Here’s my code. https://codepen.io/Minyang64/pen/jLpQzE?editors=1011

Adding https://cors-anywhere.herokuapp.com before the url and that didn’t solve the problem. What else can I do besides changing the api?


#2

The API is responding correctly but you are assigning the “lat” and “lon” values during an asynchronous call (the .getCurrentPosition), therefore they are undefined when you are creating your URL.

Wrap your call to the API in a function and call that function inside the .getCurrentPosition method. Everything should be good then.

Something like this:

navigator.geolocation.getCurrentPosition(function(pos){
      lon = pos.coords.longitude;
      lat = pos.coords.latitude;
      getWeather(lon, lat);
});
getWeather(lon, lat) {
var api = 'your_url';
  $.getJSON(api, function(data){
      // Do stuff with data
  });
}

#3

Problem solved. I really appreciate it. Thanks a million.