1

i try to do the following but it doesn't work on IE11 in my case?

var request = new XMLHttpRequest();

function doStuff()
{
 console.log("next request");
 doRequest();     
}

function doRequest() {
 request.open("GET","http://127.0.0.1/poll.php", true);
 request.onloadend = doStuff;
 request.send();
}

doRequest();

The PHP script poll.php is sleeping for one second. Now the Point: The Egde and Chrome are requesting round about once per second but the IE doesen't, it spams the log with 1000 requests per second AND they dont even got execute.

If i remove the endless Loop, the IE is doing one request, if it is a endless Loop, the IE is doing nothing except spamming the log.

Hope u can unterstand me and give me a hint, how to solve my Problem.

Best Regards.

Dennis
  • 13
  • 3
  • errors in the console perhaps? because IE is pretty retarded - using `onloadend` would mean `doStuff` gets called regardless of any error - in the case of an error, there would be no delay, therefore the so called `spam` in the log .... what you need to determine is why IE can't make that request successfully – Jaromanda X May 24 '17 at 11:45
  • No error, the same happends when "onreadystatechange": if (request.readyState == 4) {if (request.status == 200){doStuff();}} – Dennis May 24 '17 at 12:22
  • interesting that there are no errors, and IE calls `onloadend` before the successful response, that takes 1 second to come back ... I think there is an error, and you just can't see it - have you tried NOT recursing the calls, so you get a single request to debug in the IE developer tools console -hmm, now that you've added after "when" in the previous comment, my conclusion is, IE is broken, use a setTimeout for IE – Jaromanda X May 24 '17 at 12:24
  • oh, wait, maybe IE is getting the result from cache! – Jaromanda X May 24 '17 at 12:30
  • mmm thank you, i haven't tested Jet, but it sounds right, how can i prevent this. is there any best way? – Dennis May 24 '17 at 13:02
  • add a `?_=somerandomnumber` to the URL (use `Math.random()` for the random number) - OR, have your server send appropriate responses to tell the browser to not cache the result – Jaromanda X May 24 '17 at 13:07
  • Ok, thank you, Problem is solved but i dont know how to mark this question as "solved" ;) ...i am new here. – Dennis May 24 '17 at 13:38
  • I can post an answer – Jaromanda X May 24 '17 at 13:46

1 Answers1

0

It seems IE caches the request, therefore the 2nd and subsequent requests finish immediately - after running the code once, even the first request would do so

As a side note, I'm actually surprised other browsers don't cache XHR requests - at least, they don't seem to if the question is an accurate reflection of browser behaviour!

One solution is to add a random "search" (or query) string to the URL

function doRequest() {
    request.open("GET","http://127.0.0.1/poll.php?_"+Math.random(), true);
    //                                           ^^^^^^^^^^^^^^^^^
    request.onloadend = doStuff;
    request.send();
}

Another would be to have the server respond with response headers that tell the browser "don't cache this" - in the case of PHP (because the request clearly is made to 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");

(shamelessly "borrowed" from https://stackoverflow.com/a/13640164/5053002 - so if it's wrong don't blame me, I haven't used PHP in a few years)

Jaromanda X
  • 47,382
  • 4
  • 58
  • 76