I tried to pass the results of an ajax .done() query to an object or variable outside of the .done function. Unfortunately, it seems impossible to read the variable outside of the function because every time I try to pass it to a global variable it get's the value "undefined". When I alert the variable inside the .done function, the value is read. Below you find my Code:
var result;
//doing the ajax call to a .php file
var dO = $.ajax({
url: "/php-sites/fetch_ressources.php",
type: 'post',
dataType: 'json',
data: {
operation: "fetch_name"
}
});
//fetching the result with done
$.when(dO).done(function(data) {
//alert works here
result = data;
alert(result);
});
//alert doesn't work here
alert(result);
I'd like to know if the only solution is to pass the value to a DOM-Object and read it with another function. I'm looking forward to your answers! Thanks!