I'm trying to set the results of an ajax request so that it is available globally throughout my code. I originally tried just wrapping the request in a function, returning the data and then setting a global variable to that function call, but it just returned as undefined. I have no idea how to proceed.
var myId = getMyId();
getMyId();
function getMyId(){
$.ajax({
url: '/who_am_i',
method: 'GET',
dataType: 'json',
success: function(data) {
return data;
}
});
}
console.log(myId);
If you need code to understand my question, The code above does not work. I'm trying to find one that does