Weather App - Weather API callback not working

I’ve been wracking my mind for ages as to why the Weather API is still not working. I am using similar syntax as the geolocation API, and that is rendering just fine. At this stage I’m just trying to ensure I can pull the data in correctly, after which I will start on the styling and frontend.



      <meta charset="UTF-8">
      <title>Today's Weather</title>

   <p id="geoLoc"></p>    
    <p id="currTemp"></p>

And my JS

//geolocation via freecodecamp exercise
if (navigator.geolocation){
      $("#geoLoc").html("latitude: " + position.coords.latitude + "<br>longitude: " + position.coords.longitude);

//jquery current weather via openweathermapp
var lat = position.coords.latitude;
var long = position.coords.longitude;
var weatherURL = ""+lat+"&lon="+long+"&APPID=****MyKEY****";
     $('#currTemp').html("current temp: " + data.main.temp);


Are you showing any errors in the console? Are you developing locally or on CodePen?

There where 2 main problems I found using openweathermap for mine. First you need to include an api key in the URL otherwise calling from a different domain is rejected, you can get that by signing up for free on their site. The other problem was that it wouldn’t work if I was making the call from a HTTPS site. I’m not sure how to solve this problem though so if any one has I would appreciate being pointed in the right direction. For now I’m just detecting that the site is loaded over HTTPS and displaying a message to say it wont work with it.

This thread is also related to the problems:


Well, it’s not exactly JSON/JSNOP problem ([for seeing API side] ( They insist that https services are for commercial use only.

IIRC, I’ve tried few methods of crossloading and failed. However, you are asked to build functionally similar website and FCC example does not work if loaded through https either.

p.s. I would suggest using, it has limit of uses (5k/month for rfee key) but serves through https.


Thanks for all your feedback thus far.

I am currently developing in CodePen, and it doesn’t appear to have any console errors. I’m aware that the geolocation code does not work in Chrome but does work in FireFox, Opera etc.

I do have an API key set up for Open Weather, and have successfully tested the URL (sans variables etc.) in my browser window and it does pull localized weather data.

Is there anything wrong with my JS code? I’ve been staring at it for days and I’m still stymied.


1 Like

Post a link to your project for many helps.

This is the CodePen for the project.


To debug promises, make sure you have a .catch() at the end of your promise chain. This was illuminating:

     $('#currTemp').html("current temp: " + data.main.temp);

  }).catch(function(error) {console.error(error)});


This showed me

The first error is fixed by switching the order of jQuery and Bootstrap in your CodePen’s JS declarations. The second error is just because you haven’t defined position anywhere in the promise’s scope.

1 Like

Hey I’ve had many of the same issues. You should have a look at this forum post
And you can see my project link there as well for a working example.

1 Like

Thanks for the reply. I’ve added the .catch to my code, and am checking the console in CodePen and it is not showing anything. Where exactly do you access the ouput from the .catch? I want to make sure I fixed the first JQuery/Bootstrap error after I reordered them in the JS quick-add lightbox.

By promise scope, do you specifically mean the Weather API function code or the geolocation code?
Would moving the geolocation function into the Weather API function fix the second error regarding the position variable?

Thanks, Josh. I had a look at the link you provided, are you saying you need to use a different weather API (Heroku hosted) to get this app to work, instead of the recommended Open Weather API?

I’m kind of new at this but isn’t the actual data stored in the callback function? If I’m right adding ‘&callback=JSON_CALLBACK’ to the end of the URL should fix it.

Check your browser console rather than the CodePen console, or change the console method to console.log(). I’m not sure that CodePen has an error output.

As fo the position variable, you’re trying to use it on lines 36 and 37, but it’s only defined within the function from lines 30 to 32.

Thanks for the tip, I tried it, but it didn’t work.

var weatherURL = ""+lat+"&lon="+long+"&APPID=*****MYKEY*****&callback=JSON_CALLBACK";

No, to be able to use the Open Weather API on HTTPS you need to use a CORS server(Heroku Hosted). You do that like so:
"… "
In other words you simply put “” before your openweather API call. This will basically relay your request and the response off their servers. It’s a work around that is needed because navigator.geolocation.getCurrentPosition() will only work with HTTPS on many browsers.
Here is how I did it: (FYI I replaced my API key with 0s and update and itFailed are functions I have already declared)

function setpos(pos){ //Get info from with position info

Here is a link to my project if you want to take a look at a working example.


Ugh. I’m struggling as well. Every time I try and run my code, I get a response in the console (computer console, not codepens). actual lat)&lon=(my actual lon)
When I take copy and paste the API call, it shows up just fine. I took out the https, like a lot of people suggested, but maybe it still has something to do with that…

Here’s my code:

(Sorry it’s not cleaned up, was going to do that after I have it working)

don’t start address with ‘api’… and use’http://api instead’… maybe.

Just tried…still didn’t work. Thanks for the suggestion though.

If you make sure that you’re connecting to CodePen through non-secure HTTP, this code works well:

$(document).ready(function() {
  var url = '';
  var appID = 'b11c7b33ce8e8e12ecf30c57a7083f5f';
  var lat = '';
  var lon = '';

  $.getJSON('', function(loc) {
    lon = loc.lon;
    lat =;
    $.getJSON('' + appID + '&lat=' + lat + '&lon=' + lon, function(weather) {



The problem is that anyone who connects via HTTPS isn’t going to get the weather, and furthermore it’s going to fail silently. That’s bad news. I want to love OpenWeatherMap, and while I understand their decision to block SSL connections for their free accounts, it sucks for students. has a free account option that caps out at 1,000 calls per day and will let you use HTTPS.


Would you suggest using an alternate geolocation API since there a browser compatibility issues with the example from the FCC exercise - navigator?

Functionally the code you provided is identical to mine, and it works, so I feel it might be more geolocation related than the Open Weather API. Is my assumption correct?