0

因为我的标题太短,我会解释的更清楚。我在 JavaScript 中创建了一个代码。我有两个选项可以运行:

1)在机器上运行:简单点击进入html文件。

2) 在本地服务器上运行:意味着我启动 Apache,并在 localhost 中启动这个 html 文件。

http://localhost:85/Javascript/index.html例如)

当我选择解决方案 1 时,什么都没有发生。当我选择解决方案 2 时,如我所愿。但我不知道为什么。

这是我的代码。目的:获取一个 json 文件并对其进行处理。

<script>
        window.onload = function(){
            var url = "http://localhost:85/javascript/json1.json";  // problem here
            var request = new XMLHttpRequest();
            request.open("GET", url);
            request.onload = function(){
                if (request.status == 200){
                    update(request.responseText);
                }
            }
            request.send(null);
        };
function update(responseText){ // some code here }
</script>
4

2 回答 2

3

您不能使用 AJAX 来读取来自不同域的内容。

运行的 Javascriptfile://whatever无法读取localhost:85

于 2012-06-12T18:56:21.503 回答
2

您是否将此行替换为服务器的原始路径?

var url = "http://localhost:85/javascript/json1.json";

var url = "http://10.0.0.X:85/javascript/json1.json"; // Did you change the right path?

并确保该页面没有被file://协议调用!

于 2012-06-12T18:56:24.723 回答