我複製了http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first的示例,並將其保存在本地計算機上的test.html中。是的,我也做了一個ajax_info.txt文件,但是當我點擊按鈕什麼也沒有發生,並且在w3schools工作正常。有任何想法嗎?AJAX示例不能在本地工作
下面是複製的代碼:
<html>
<head>
<script type="text/javascript">
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>
Ajax在本地不起作用。你需要有一個網絡服務器發佈。 – Garvin
我認爲同樣的事情,但是...如果他在他的本地機器上有一個Web服務器用於測試目的,那該怎麼辦? –