I'm having trouble retrieving data from a JSON file using JavaScript. The console is showing the result as undefined
.
The GET request seems to be successful and the function is executing, but I'm unable to access the response data.
function fetchDataFromJSON(url) {
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.setRequestHeader("Content-type", "application/json");
request.onreadystatechange = function() {
if(request.readyState == 4 && request.status == 200){
return JSON.parse(request.responseText);
}
};
request.send();
}
var jsonData = fetchDataFromJSON('questions.json');
console.log(jsonData); //undefined