I'm trying to access data from a JSON file but the result of the console is undefined
.
The GET response is good, the function works but I don't know how to access the response.
function AJAX_JSON_Req(url) {
var AJAX_req = new XMLHttpRequest();
AJAX_req.open("GET", url, true);
AJAX_req.setRequestHeader("Content-type", "application/json");
AJAX_req.onreadystatechange = function() {
if(AJAX_req.readyState == 4 && AJAX_req.status == 200){
return JSON.parse(AJAX_req.responseText);
}
};
AJAX_req.send();
}
var q = AJAX_JSON_Req('questions.json');
console.log(q); //undefined