Below is my code in a JavaScript file that is included in an HTML file. When I console.log msg I can see there are 100 items in the array (see screenshot), however dataArray is still empty after the last console.log(dataArray).
I don't get any errors or things like that so it's hard for me to debug this.
function loadPosts() {
var dataArray = new Array();
var root = 'https://jsonplaceholder.typicode.com';
$.ajax({
url: root + '/posts/',
method: 'GET',
success:function(msg){
dataArray = msg;
}
});
console.log(dataArray);
}
window.onload = loadPosts;
dataArray = msg;
todataArray = msg.d;
dataArray
to referencemsg
, and the initial array is thrown out. You aren't setting the actual elements of the array to anything.loadPosts()
function does nothing except fetching the data using ajax call. You didn't returned thedataArray
in that function. Didn't used thedataArray
for any purpose.