I can’t push elements to works array. Console log is returning proper objects, but they won’t be pushed to the array… Here is my code:
var works = new Array();
$(window).ready(function()
{
$.getJSON('AJAX/getWorks.php', function(data) {
$.each(data, function(key, val) {
console.log(val);
works.push(val);
});
});
console.log(works);
});
And the json object:
Object
date: "2012-04-08 17:53:58"
description: "sadasd"
id: "2"
link: "sadasd"
name: "dsad"
objects: null
position: "2"
__proto__: Object
Anybody see what I am doing wrong? Thanks in advance for answers…
You’re logging the array too early in your code. The
console.logwill run before the ajax request has finished becauseajaxis asynchronous.EDIT
If you want to use that variable after the ajax request you can do the following
Create a function to house the ajax request
Then you can do the following to assure that the ajax request is complete.