-2

I have been using an HTTP request in a JS file to retrieve information from a local JSON file. It works just fine on my Windows computer in Firefox and Chrome, but when run on a Mac, the Chrome debugger throws an error saying that Cross origin requests are only supported for HTTP... My HTTP request code is as follows:

var xhr = new XMLHttpRequest();

xhr.open("GET", "sample.json", true);

xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
        var status = xhr.status;

        if ((status >= 200 && status < 300) || status === 305) {
            var myData = JSON.parse(xhr.responseText);
            window.myData = myData;
            showAll(myData);
        }
    }
};

xhr.send(null);

Any ideas? Thanks

Mike
  • 33
  • 1
  • 8

1 Answers1

0

Yes, this is a security issue. You need to run this on a server, and the file:/// protocol is not supported for such kind of requests! AJAX is a proper HTTP request response kind of concept and you cannot use file:/// protocol or use different protocols for the transaction.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252