【问题标题】:W3Schools AJAX First Example. Worked on FF but Failed on ChromeW3Schools AJAX 第一个示例。在 FF 上工作但在 Chrome 上失败
【发布时间】:2015-09-11 14:34:46
【问题描述】:

我刚开始从 W3Schools 学习 AJAX。 复制粘贴他们的第一个示例代码并在我的 PC 上运行。 它适用于 FF,但在 Chrome 上失败。 谁能告诉我为什么会这样?

我的 FF/Chrome 屏幕截图

Source from w3schools.com

<!DOCTYPE html>
<html>
<head>
<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","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>

【问题讨论】:

  • 一句忠告,W3Schools 与 W3C 相关联,因此它不是“官方”,因为名称可能会让您相信。虽然我不认为它像以前那样糟糕,但值得一读:w3fools.com
  • @Jon P. 我读了你的链接,但它并没有解决我的问题。

标签: html ajax


【解决方案1】:

您需要使用此参数启动 chrome 才能访问本地文件

--allow-file-access-from-files

例如:c:\Browser\chrome.exe --allow-file-access-from-files

【讨论】:

  • 我试过这个(从 cmd 启动 chrome 并带有这些附加词),但它没有用。和以前一样出现在控制台中的错误响应,现在不再出现。但内容仍然没有改变。
猜你喜欢
  • 1970-01-01
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 2012-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多