the getJSON call is asynchronous and so your lat and long variables are not ready by the time you call your second getJSON. Turn off asynchronous behavior using the full $,ajax form. You can try this
// coords api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&APPID=cd231340ca4330baff41d035aa2ed150
// get geo location
$(document).ready(function(){
var lat;
var long;
var url;
// get user location
$.getJSON("http://ip-api.com/json", function(data){
lat = data.lat;
long = data.lon;
url = "api.openweathermap.org/data/2.5/weather?lat="+lat+"&lon="+long+"&APPID=cd231340ca4330baff41d035aa2ed150";
console.log(data);
console.log(getWeather(lat, long));
});
});
function getWeather(latitude, longitude){
var url = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "&appid=4fe5f53682ebc7dd08d794486af04245";
var json = $.ajax({
url: url,
async : false,
}).responseText;
return (JSON.parse(json));
}
I have wrapped your weather call in a getWeather function with async behavior set to false. This way the function will not continue to load until the url is ready.
Thanks for your reply! I’m a little confused by your code though.
I’ve seen many other campers users a getjson call inside of another getjson call and it works fine. For example, a fellow campera Hasan Sönmez has a great weather app that does just that.
How come when I do the same it doesn’t work?? Also, I’m confused as to where you define the latitude and longitude variables. I use lat and long variables but define them inside the first getjson, so wouldn’t the getWeather ajax call not be able to access them because of the variable scope chain?
Thanks again for your reply and helping me with this!
Maybe this is issue url = "api.openweathermap.org/data/2.5/weath
U dont have prefix http or https
Maybe it will not work with https cause u are not premium user of their service
it is easier to debug ur request on codepen, but i would like u to try first adding prefix http://