JQuery Ajax - How to Detect Network Connection error when making Ajax call

前端 未结 8 2155
闹比i
闹比i 2020-11-27 04:02

I have some Javascript JQuery code that does an Ajax call to the server every 5 mins, it\'s to keep the server session alive and keep the user logged in. I\'m using $.

相关标签:
8条回答
  • 2020-11-27 04:44

    You should just add: timeout: <number of miliseconds>, somewhere within $.ajax({}). Also, cache: false, might help in a few scenarios.

    $.ajax is well documented, you should check options there, might find something useful.

    Good luck!

    0 讨论(0)
  • 2020-11-27 04:45

    here's what I did to alert user in case their network went down or upon page update failure:

    1. I have a div-tag on the page where I put current time and update this tag every 10 seconds. It looks something like this: <div id="reloadthis">22:09:10</div>

    2. At the end of the javascript function that updates the time in the div-tag, I put this (after time is updated with AJAX):

      var new_value = document.getElementById('reloadthis').innerHTML;
      var new_length = new_value.length;
      if(new_length<1){
          alert("NETWORK ERROR!");
      }
      

    That's it! You can replace the alert-part with anything you want, of course. Hope this helps.

    0 讨论(0)
提交回复
热议问题