0

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.

brienna
  • 1,415
  • 1
  • 18
  • 45
  • What error are you seeing? – Mike Cluck Jan 27 '16 at 17:31
  • Edited to add what error I am seeing – brienna Jan 27 '16 at 17:32
  • What is `Quiz interactivity.js` ? – guest271314 Jan 27 '16 at 17:34
  • My JavaScript file that contains the XMLHttpRequest. – brienna Jan 27 '16 at 17:35
  • is questions.json in the same folder as the file executing this code? – P. Jairaj Jan 27 '16 at 17:43
  • I guess you just are being "confused" over your own console.logs. There will always be `onreadystatechange`'s even if the file not is finished loading - if you make a `console.log(allQuestions)` you will see that the request eventually actually successes. – davidkonrad Jan 27 '16 at 17:58
  • @davidkonrad: After the file finishes loading (when `httpRequest.readyState === XMLHttpRequest.DONE`), the flow of control jumps to the `else` statement and outputs that there was a problem with the request. I tried putting a `console.log(allQuestions)` on the line after `var allQuestions`, but there is no output. – brienna Jan 27 '16 at 18:03
  • @BriennaHerold - I copied and tried out your code and it worked right away - despite 3 times "_The request is still not ready_" and a `200` in the console, `questions.json` is sucessfully loaded into `allQuestions`. – davidkonrad Jan 27 '16 at 18:30
  • Hmm. Did you use a server or the same switch as I did? @davidkonrad – brienna Jan 27 '16 at 18:31
  • 1
    tried ther code in www on a apache server using chrome - actually I have never heard about that switch before, and something tells me that it is obsolete and should be avoided -> http://stackoverflow.com/questions/24826544/chrome-allow-file-access-from-files-no-longer-working-was-using-to-see-webgl-th – davidkonrad Jan 27 '16 at 18:36

0 Answers0