Code not working?

Code not working?
0

#1

how come the first console.log(data) gets logged into the console and is a correct url but the second console.log(data2) doesn’t? any ideas? I don’t want to waste time writing more code if its not getting the second json.
// 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);

 $.getJSON(url, function(data2){
  console.log(data2);
 }); 

// end of first getjson ip-api/json
})
//end of doc ready
});


#2

Hey @runsf, did you have this on codepen? I think i know what’s wrong but i’ll need to test it over there.


#3

Yeah it was in code pen… I’ve been having a lot of issues working on these projects in it… It seems buggy


#4

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.


#5

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!


#6

Can post ur link of codepen

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://


#7

here it is, theres not a lot here yet… I was just running some tests with the console to make sure i was getting all the api data correctly


#8

you are a legend!!! thank you my friend it was the prefix!! Thanks so much for your help! lesson learned!