我一直在玩Javascript,现在我来到了Ajax。我试图写的很简单的脚本,将获得该文件的内容 - 与id=test
打印在div
的txt
文件内容。这是脚本:为什么我的基本Ajax脚本不工作?
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET" , url ,false);
xmlhttp.send(null);
document.getElementById('test').innerHTML = xmlhttp.responseText;
}
当我使用它这个网站上:
<div id="test" name="test"> HELLo </div>
<button type="button" onclick="loadXMLDoc('test1.txt')">ClickMe1</button>
有了这个脚本HELLo
是什么取代 - 脚本清空容器。
也许我失去了一些东西微不足道,但我还需要安装PHP?我不这么认为,但是......我不确定这里发生了什么。当我调试时,xmlhttp
一直是空的。为什么?
我会建议使用框架,如jQuery阿贾克斯,它会自动处理大量不同的浏览器,甚至同一个浏览器,但不同版本之间的不一致。 – alykhalid 2012-03-09 12:52:04
你会得到什么错误? – 2012-03-09 13:07:46
@ClydeLobo正如我发现刚才 - 我的'xmlhttp.status'是整个时间'0'。这是为什么?我应该得到'200' – Patryk 2012-03-09 13:26:14