I'm trying to use an ajax response in getLocation()
to save variables lat and long for use in getWeatherReport()
but both variables are empty strings when I console.log them in the 2nd function.
To my understanding creating the variables outside of the scope of the functions would allow them to be updated by the first function and then have those updated values plugged into the second one. If anyone could explain where I went wrong and why this isn't the case it would be very much appreciated. Thank you!
To Clarify the AJAX call is working fine.
console.log(lat + ", " + long); Shows the expected results of a latitude and longitude.
JS
var lat = "";
var long = "";
function getLocation(){
console.log("Getting location with ajax");
if(window.XMLHttpRequest){
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", function(){
console.log("loading...");
console.log(xhr.responseText);
var response = JSON.parse(xhr.responseText);
console.log("Parsed response: ");
var lat = response.latitude;
var long = response.longitude;
console.log(lat + ", " + long);
return lat;
}, false);
xhr.addEventListener("error", function(err){
console.log("Could not complete the request");
}, false);
xhr.open("GET", "http://www.telize.com/geoip", true);
xhr.send();
console.log("Requestiong location info...");
} else {
console.log("Unable to fetch location info from dropbox.");
}
}
function getWeatherReport(){
console.log("Weather Report Location");
console.log(lat + long);
}
getLocation();
getWeatherReport();