I have a problem with this code. This happens: just launch the code from console.log I get null, if I press the update button function works.
why is this happening? how do I fix?
var urljson = "data_json.php";
var data = null;
var jqXHR = $.ajax
({
type: "GET",
url: urljson,
dataType: 'json',
success: successHandler
});
function successHandler(result) {
data = result;
}
function update(){
var jqXHR = $.ajax
({
type: "GET",
url: urljson,
dataType: 'json',
success: successHandler
});
function successHandler(result) {
data = result;
}
}
$(document).ready(function(){
console.log(data) //// null
});
document.getElementById('update').addEventListener('click', function() {
update();
console.log(data) //// Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, other 3213… ]
});