Pass the returned array from (Ajax output) to use as GLOBAL VARIABLES everywhere
I'm trying to get back from Ajax output , two variables in an array, for use in other separate functions elsewhere:
But fails to determine How to pass this to new functions?
My attempt:
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 like this:
jQuery(document).ready(function () {
alert(ip)
});
Sorry if I wasn't clear enough about this!
Thank you very much in advance!
source to share
I would like to keep the promise here:
var ip = {};
var country = {};
var getstuff = {};
function callback(response) {
ip = response.ip;
country = response.country_code;
// not really needed return [ip, country];
}
function getVisitorData() {
getstuff = $.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true
}).done(callback)
.fail(function (response) {
alert("IP thing didn't work.");
});
}
jQuery(document).ready(function () {
getVisitorData();
getstuff.done(function () {
alert(ip + country);
});
});
Here's it in action: http://jsfiddle.net/MarkSchultheiss/7bmnsvay/
source to share
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(data) {
var ip = data[0];
var country = data[1];
return [ip, country];
}
jQuery(document).ready(function () {
getVisitorData(callback);
});
Whether manipulating data into a callback
function is not in any other function. For more information visit Callback Function
source to share
I don't know why you need to return an array in your callback. Since ajax is asynchronous, it won't wait for a response, it will execute the rest of the instructions. So the return value will give you undefined. You can only use / assign a response after a response.
function getVisitorData(callback) {
return $.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true
});
}
jQuery(document).ready(function () {
$.when(getVisitorData())
.done(function(respone) {
var ip = response.ip;
var country = response.country_code;
//here you can trigger a custom event with the values, wherever you need, just bind the event and use the data.
$(document).trigger("responseDataPublished", [ip, country]);
})
.fail(function(response){
alert("IP thing didn't work.");
});
});
source to share
This is a common problem that we are not aware of, before your service raises success or failure, the line next to your service call is done, so you need to set some timeout
// Create a global object. var objMyData = {} global object
;
function getVisitorData () { function call
$ .ajax ({`Ajax type:" GET ", url:" http://freegeoip.net/json/ ", dataType:" json ", async: true, success: function (response) { // Store ip and country code in this object.objMyData.ip = response.ip; objMyData.Country = response.country_code;}, error: function (response) {alert ("IP device is not working");}}) ; }
jQuery (document) .ready (function () {// Now we pass the objMyData object as a parameter to any function getVisitorData (); method call
setTimeout (function () {console.log (objMyData);}, 500);});
source to share