0

Problem: My async ajax call is stalling links on the page (but only links targeting the same domain) until the ajax has completed. To confirm this I've added a sleep(10) into the php file the ajax is referring to, so the ajax call takes at least 10 seconds to complete.

After 10 seconds the chrome console reports

XHR finished loading

and the link is then activated.

However, (and this is where it's weird) links targeting other domains are activated immediately - the ajax is terminated instantly on click and the chrome console is reporting:

XHR failed loading

and the link is activated.

How can I insure local links are treated as the remote links are - immediate.

Here is the ajax call:

$.ajax({
     async:true,
     url: "/ajax/ajax-get-static-calendar",
     type: "GET",
     data: "cid=" + $('#sm-calendar').attr('data-cid')+ "&numDays=" + $('#sm-calendar').attr('data-numdays'),
     success: function(msg) {
        console.log('ajax done');
     }
});
nzmuzzer
  • 99
  • 1
  • 3
  • 1
    Please explain what you mean by "stalling links". Do you mean the browser becomes unresponsive, the server is not responding, both? – Yogi Feb 10 '16 at 22:03
  • @Roberto When I say "stalling links"; the link fires but the redirect does not happen UNTIL the AJAX is complete. Which is why I added the 10second delay so I could confirm this visually. Once the AJAX request is complete the link immediately fires. So it's like the click is being registered and queued until the AJAX is done. Remote links fire immediately. – nzmuzzer Feb 10 '16 at 22:29
  • Are you sure that it's not an issue with your server response time? Try putting: `window.onbeforeunload = function() { return "I AM GOING NOW"; };` in your code; if it fires when you click the link, then the issue is likely a problem with your server being frozen until it completes whatever operations happening on "/ajax/ajax-get-static-calendar". – Rob M. Feb 10 '16 at 22:37
  • @cale_b Similar but different question, however the answer in that question which suggests using session_write_close() in the php actually works. But why does it work? Is there only 2 php sessions allowed at any time or something? – nzmuzzer Feb 10 '16 at 22:39
  • @Rob M, yes it fires the alert immediately. So why does the server stall the redirect only if the redirect target is on the local server? It's only a sleep(10); call, no heavy processing. Surely the browser should redirect immediately if that is the instruction from the user, even if it leaves the server processing the ajax on it's own? – nzmuzzer Feb 10 '16 at 23:50
  • `sleep(10)` is imitating heavy processing (at least from the AJAX request's (and possibly Nginx/Apache's) perspective). There are a lot of potential causes of this, your HTTP Keep Alive settings, if you're running PHP-FPM vs. PHP, etc. It sounds like your connection to the server is being held open and waiting for that to be released before it can move on to the next connection. You can turn off keep alive in your server to explore that possibility. – Rob M. Feb 11 '16 at 00:00
  • Thanks Rob. yes, you're right, the connection to the server is being held open and waiting for that to be released. I can see that visually as the instant the XHR is complete the link redirects. I can replicate this constantly. I've since added session_close() as suggested by @cale and that fixes the issue, I just don't understand WHY it fixes it. – nzmuzzer Feb 11 '16 at 00:52

0 Answers0