我找到了一个很好的教程:
tutorial
但它在本地不起作用.
问题是,responsetext返回我的整个PHP代码.
我双击我的ajaxclock.html并使用Firefox.
令人惊讶的是,它适用于服务器.
这里的代码:
ajaxclock.html
/p>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
AJAX TutorialAktualisieren
的script.js
var req = getXmlHttpRequestObject();
window.onload = getTime();
function getXmlHttpRequestObject()
{
if(window.XMLHttpRequest)
{
return new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert('Ajax funktioniert bei Ihnen nicht!');
}
}
function getTime()
{
if(req.readyState == 4 || req.readyState == 0)
{
req.open('GET','ajaxclock.PHP',true);
req.setRequestHeader("Content-Type","text/plain");
req.onreadystatechange = setMessage;
req.send(null);
}
}
function setMessage()
{
if(req.readyState == 4)
{
var response = eval('(' + req.responseText+ ')');
document.getElementById('time').innerHTML = response.time;
}
}
ajaxclock.PHP