I am trying to load from a PHP
file using ajax
, some JSON
objects but my problem is that my javascript
code is not working on the correct order. Here is my code:
var requests;
$(document).ready(function(){
setInterval(function(){
$.ajax({
url: "test2.php",
success: function(result){
requests = JSON.parse(result);
alert(requests);
}
});
})
});
alert(requests);
Here is the first alert i'm getting on page load and here is the second alert
My question is why the alert in the last line is executed before the ajax
Note: this is a small example of my project, my real error is that i cannot load some arrays i need using ajax, because it shows as undefined at console, even if the ajax is in the beginning of the script.