JSON HTTP 请求在 Mac OSX 上不工作

JSON HTTP Request Not Working on Mac OSX

我一直在 JS 文件中使用 HTTP 请求从本地 JSON 文件中检索信息。它在我的 Windows 电脑上用 Firefox 和 Chrome 工作得很好,但是当 运行 在 Mac 上时,Chrome 调试器抛出一个错误说 Cross origin requests are only supported for HTTP... 我的HTTP请求代码如下:

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);

有什么想法吗?谢谢

是的,这是一个安全问题。您需要在服务器上 运行 此请求,并且 file:/// 协议不支持此类请求! AJAX 是一种正确的 HTTP 请求响应概念,您不能使用 file:/// 协议或对交易使用不同的协议。