I'm trying to retrieve an array from a JSON file named questions.json (that is in the same folder as the JavaScript file). To overcome the same origin policy, I've allowed XMLHttpRequest to access local files using the Chrome --allow-file-access-from-files
switch.
I've looked up other questions related to this, but I'm still not sure why my request fails. Is the failure related to my AJAX call to localhost? Is what I am doing a valid HTTP request? Did I not overcome the same origin policy?
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
console.log(httpRequest.status);
if (httpRequest.status === 200) {
var allQuestions = httpRequest.responseText
} else {
console.log('There was a problem with the request.')
}
} else {
console.log('The request is still not ready.')
}
}
httpRequest.open('GET', 'questions.json', true);
httpRequest.send(null);
My console shows that the request is still not ready (3 times), that XHR has finished loading, that the httpRequest.status
is 0, then that there was a problem with the request.