【发布时间】: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. 我读了你的链接,但它并没有解决我的问题。