Search code examples
javascriptajaxshared-hosting

AJAX not pulling on bluehost


I have two simple pages.

test.php:

<div id="demo"></div>


<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("demo").innerHTML =  this.responseText;
    }
  };
  xhttp.open("GET", "sse.php", true);
  xhttp.send();
}

var myVar = setInterval("loadDoc()", 2000);
</script>

sse.php

<?php
echo time();
?>

Problem is - in my private PC - it works perfect. every 2 seconds, sse.php is pulling, and demo-div changes accordingly.

BUT, in my Bluehost website, same script - doesn't work. it pulls one time (after two seconds) and that's it - no more.

Funny thing is, if i refresh the sse.php manually (in another tab) the demo div content does change! I tried to figure out what's the issue, but i'm out of ideas.

Any ideas?

Thank you.

enter image description here


Solution

  • After further research - adding those lines in the top of my sse.php file and refresh this page, solved my problem. Thanks.

    <?php
    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    
    echo time();
    ?>