I'm trying to return from the Ajax output, two variables in array, to use in another separate functions in another places:
But can't figure out How to pass it out to the new functions?
My attempt here:
var ip,country;
function getVisitorData(callback) {
$.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true,
success: function (response) {
ip = response.ip;
country = response.country_code;
callback(response);
},
error: function (response) {
alert("IP thing didn't work.");
}
});
}
function callback(response) {
return [ip, country];
//alert(ip + country); /* <- it works here */
}
getVisitorData(callback);
jQuery(document).ready(function () {
var data = getVisitorData(callback);
var ip = data[0];
var country = data[1];
alert(ip + country);
});
Update:
I want to use the output as GLOBAL VARIABLES !
So, get/use it just like that:
jQuery(document).ready(function () {
alert(ip)
});
Sorry if I wasn't clear enough about that!
Many thanks in advance!