I have already read Surprised that global variable has undefined value in JavaScript.
But my problem is differ because I have not created same variable name inside..
$(document).ready(function() {
var all_ap;
var url = "http://localhost/example/php-code/json.php";
$.getJSON(url, function(result) {
all_ap = result;
console.log(result);
});
console.log(all_ap);
});
These are the console results.
undefined
(6) [{…}, {…}, {…}, {…}, {…}, {…}]
Seems like console.log(all_ap);
works before console.log(result);
according to results of above.
Why is that? How should I make all_ap
variable works?