I am trying to complete the local weather app using the ipinfo API.
I access the city,country and coordinates likeso
<script src="https://code.jquery.com/jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$.get("http://ipinfo.io" ,function(data){
console.log(data.city+' '+data.country);
console.log(data.loc);
},"jsonp");
});
The output comes correctly with the city,country and coordinates.
Now I am trying to write the same as above using functions
<script src="https://code.jquery.com/jquery.js" type="text/javascript"></script>
<script>
function getCityCountry() {
var cityCountry=''
$.get("http://ipinfo.io" ,function(data){
cityCountry=data;
},"jsonp");
return cityCountry.city+' '+cityCountry.country
}
function getLocation() {
var location=''
$.get("http://ipinfo.io" ,function(data){
location=data;
},"jsonp");
return location.loc;
}
$(document).ready(function(){
$.get("http://ipinfo.io" ,function(data){
console.log(getCityCountry());
console.log(getLocation());
},"jsonp");
});
</script>
Now the output comes as undefined for all the above mentioned value.
What am I doing wrong?What is the correct way to define and use functions in jQuery?