简体   繁体   English

Ajax无法在网站上使用

[英]Ajax isn't working on website

For my personal site I want to insert a simple ajax server clock into my website, but for some reason it's not showing up in the header. 对于我的个人网站,我想在我的网站中插入一个简单的ajax服务器时钟,但是由于某种原因,它没有显示在标题中。 Here's the Javascript code 这是Javascript代码

var httpxml;
try {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
} catch (e) {
  // Internet Explorer
  try {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      alert("ur browser doesn't support ajax m8, try reinstalling windows 95");
      return false;
    }
  } 
}

function stateck() {
  if(httpxml.readyState==4) {
    document.getElementById("time").innerHTML=httpxml.responseText;
  }
}

var url="ajax-server-clock-demock.php";
url=url+"?sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
tt=timer_function();
}

function timer_function(){
  var refresh=1000; // Refresh rate in milli seconds
  mytime=setTimeout('AjaxFunction();',refresh)
}

And in php file 并在php文件中

<?Php
echo date("d/m/y : H:i:s", time());
?>

Lastly in the header 最后在标题中

<time>bacon</time>

I would have to see more code in context because several things can make this not work, in the mean time, there are multiple issues with your script, among others: 我将不得不在上下文中查看更多代码,因为有几件事情可以使此方法不起作用,同时,您的脚本存在多个问题,其中包括:

You did setTimeout(). 您做了setTimeout()。 That would only execute once, assuming you want the clock to update every second you need to use: 假设您希望时钟每隔一秒钟更新一次,那只会执行一次:

http://www.w3schools.com/jsref/met_win_setinterval.asp http://www.w3schools.com/jsref/met_win_setinterval.asp

Oh, also, you need to do something like: 哦,此外,您需要执行以下操作:

document.getElementsByTagName('time')[0]; 

Instead of getElementById() 代替getElementById()

OR add an id to your element like: 或为您的元素添加一个ID,例如:

<time id="mytime"></time>

And then you can call: 然后您可以致电:

document.getElementById('mytime');

got it? 得到它了?

And also, where is declared this AjaxFunction() you are calling in setTimeout? 而且,在setTimeout中调用的AjaxFunction()声明在哪里?

Also, there are much better ways to do a clock... and you should really consider using jQuery or something like that, will make your life much easier when doing AJAX, DOM manipulation and etc... 此外,还有更好的时钟制作方法...您应该真正考虑使用jQuery之类的工具,这将使您在进行AJAX,DOM操作等操作时更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM