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

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.

Adding before the url and that didn’t solve the problem. What else can I do besides changing the api?

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:

      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

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

1 Like