I'm relatively new to JavaScript, and I thought I knew how callback functions work, but after a couple of hours of searching the Internet, I still don't understand why my code is not working.
I am making an AJAX request that returns an array of strings. I am trying to set this array to a local variable, but it seems to lose its value as soon as the callback function is executed.
var array; $.ajax({ type: 'GET', url: 'include/load_array.php', dataType: 'json', success: function(data){ array = data; }, error: function(jqXHR, textStatus, errorThrown){ alert("Error loading the data"); } }); console.debug(array);
In the console, array
displayed as undefined. Can someone explain to me why this is not set and how to set a local variable in the callback function.
source share