JSON HTTP请求在Mac OSX上不起作用

时间:2015-06-16 12:32:38

标签: javascript html json macos xmlhttprequest

我一直在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);

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:0)

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

相关问题