简体   繁体   中英

AJAX only loading in IE once

I can not figure out why the following AJAX code will only load the information from my php page one time in IE, works in all other browsers with no problem.

   <script type="text/javascript">
var c=0;

function randomize(str)
{

if (str=="")
  {
  document.getElementById("dispeople").innerHTML="";
  return;
  } 
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("dispeople").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","randomize.php?q=" + Math.random(),false);
xmlhttp.send();

document.getElementById('txt').value=c;
c=c+1;
}

setInterval( "randomize()", 2000 );
</script>

I am fairly new to using AJAX and have researched this issue and implemented all the solutions I could find, any help would be greatly appreciated.

Update, 10/21/2011

I have checked out JQuery and implemented it and am still experiencing the same issue if anyone has any suggestions here is the code I am currently using:

<script type="text/javascript">
var c=0;

$.ajaxSetup ({
    // Disable caching of AJAX responses
    cache: false
 });

   setInterval(function() {
     $.ajax({
     type: "GET",
     url: 'randomize.php?ck=' + (new Date()).getTime(), 
     cache: false,
        success: function(data) {

          $('#dispeople').html(data);
     }
     });
     },2000);


</script>

Thank you

I think your jquery code is fine. It happens due to ie browser caching. Try sending a random value with your request and return the time() from php side to see if it is actually calling the php script.

Try this instead:

setInterval( randomize, 2000 );

Also you are sending nothing as a string. and the 1st part of your code:

  if (str=="")
  {
    document.getElementById("dispeople").innerHTML="";
    return;//return, only run once....
  } 

Maybe there's a reason you're not using it already, but I highly recommend checking out jQuery . That library automatically smoothes out all of these browser incompatibility issues, so you can just worry about your functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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