I'm currently working on a project where I need to fetch data from a JSON file using XMLHttpRequest and then store it in an array. However, I keep getting errors when attempting to do so.
Take a look at this code snippet that seems to be causing the issue:
var results = [];
var request = new XMLHttpRequest();
request.open("GET", "data.json");
request.onreadystatechange = processData(request, results);
request.send();
function processData(request, results) {
if (request.readyState == 4 && request.status == 200) {
var jsonData = JSON.parse(request.responseText);
results.push(jsonData.data);
alert(results);
}
}
The problem appears to occur when trying to access the 'target' value within the function, returning a value of '4' instead.