Hello everyone ! I was working on a local weather app, and I stumbled uppon a problem I cannot solve ! I’ve been scratching my head for an hour without being able to figure out what’s going on.
So, I’m calling a Jquery function to set a few variable but , even when I try to set those variable as global by not putting the famous ‘var’ beofre declaring them, I cannot use them outside the Jquery function.
Here is the code :
var country;
function getCountry(){
$.ajax({
dataType: "json",
contentType: "application/json",
type : "GET",
url: "https://fcc-weather-api.glitch.me//api/current?",
data : {
lat:"48.8566",
lon:"2.3522",
},
success : function(quote){
var temperature = quote.main.temp;
var wind = quote.wind.speed;
var pressure = quote.main.pressure;
var humidity = quote.main.humidity;
country = quote.sys.country;
document.getElementById("wind").innerHTML = wind;
document.getElementById("temperature").innerHTML = temperature;
document.getElementById("pressure").innerHTML = pressure;
document.getElementById("humidity").innerHTML = humidity;
document.getElementById("country").innerHTML = country;
},
error : document.getElementById("temperature").innerHTML = "error"
});
}
$(document).ready(function(){
getCountry();
document.getElementById("countryimg").src = "http://www.geognos.com/api/en/countries/flag/" + country + ".png" ;
});
As you can see, i’m trying to use the variable ‘country’ to get a picture of the country flag, but nothing to do, the variable doesn’t seem to work outside the AJAX function. Does anyone have any idea why it’s not working ? Thanks a lot !