因为我的标题太短,我会解释的更清晰。我用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>
[Origin-null不允许被访问控制 - 允许来源]可能的重复(http://stackoverflow.com/questions/8456538/origin-null-is-not-allowed-by-access-control-允许来源) – Quentin