2012-06-12 83 views
0

因为我的标题太短,我会解释的更清晰。我用JavaScript创建了一个代码。我有两个选项来运行: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> 
+1

[Origin-null不允许被访问控制 - 允许来源]可能的重复(http://stackoverflow.com/questions/8456538/origin-null-is-not-allowed-by-access-control-允许来源) – Quentin

回答

3

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

Javascript正在运行file://whatever无法读取localhost:85

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://协议!

相关问题