【问题标题】:Error in Chrome when i use AJAX使用 AJAX 时 Chrome 出现错误
【发布时间】:2013-06-10 06:45:09
【问题描述】:

好的,我一直在使用 Ajax,当我在 IE 中运行它时,它运行良好 这是 HTML 代码:

    <!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","https://www.dropbox.com/s/tq4qhxqtqpgjd57/ajax_info.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

问题是当我在 Chrome 中运行它时,它得到了这个错误:

XMLHttpRequest cannot load https://www.dropbox.com/s/tq4qhxqtqpgjd57/ajax_info.txt. Origin file:// is not allowed by Access-Control-Allow-Origin. 

我尝试使用 –allow-file-access-from-files 但它不起作用或者我用错了

也有人可以解释一下为什么会出现这个错误?

【问题讨论】:

标签: ajax google-chrome


【解决方案1】:

您应该在 Dropbox 设置中检查该文件 DIR 的访问权限。

【讨论】:

【解决方案2】:

我看到您已经将 JQuery 包含到您的页面中。只需使用

$('#myDiv').load("https://www.dropbox.com/s/tq4qhxqtqpgjd57/ajax_info.txt");

使用 JQuery 可以让您忽略浏览器兼容性问题,并使您的代码更具可读性。

【讨论】:

  • 作为你整个函数体的替代品。从function loadXMLDoc() {}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-20
  • 2016-03-18
  • 1970-01-01
  • 2015-10-15
相关资源
最近更新 更多