Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 $.ajax() method in JQuery. This function seems to have an 'error' property that I'm trying to use in the event that the user's internet connection goes down so that the KeepAlive script continues to run. I'm using the following code:

var keepAliveTimeout = 1000 * 10;  function keepSessionAlive() {     $.ajax(     {         type: 'GET',         url: 'http://www.mywebapp.com/keepAlive',         success: function(data)         {             alert('Success');              setTimeout(function()             {                 keepSessionAlive();             }, keepAliveTimeout);         },         error: function(XMLHttpRequest, textStatus, errorThrown)         {             alert('Failure');              setTimeout(function()             {                 keepSessionAlive();             }, keepAliveTimeout);         }     }); } 

When I run it, I'll get 'Success' popup on the screen in an alert box every 10 seconds which is fine. However, as soon as I unplug the network cable, I get nothing, I was expecting the error function to get called and see a 'Failure' alert box, but nothing happens.

Am I correct in assuming that the 'error' function is only for non '200' status codes returned from the server? Is there a way to detect network connection problems when making an Ajax call?

like image 484
Sunday Ironfoot Avatar asked Nov 13 '09 17:11

Sunday Ironfoot


People also ask

How do you handle errors in AJAX call?

The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.

How do I know if AJAX code is working?

ajax() : $. ajax({ type: 'POST', url: 'page. php', data: stuff, success: function( data ) { }, error: function(xhr, status, error) { // check status && error }, dataType: 'text' });

Why is AJAX success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.


1 Answers

// start snippet error: function(XMLHttpRequest, textStatus, errorThrown) {         if (XMLHttpRequest.readyState == 4) {             // HTTP error (can be checked by XMLHttpRequest.status and XMLHttpRequest.statusText)         }         else if (XMLHttpRequest.readyState == 0) {             // Network error (i.e. connection refused, access denied due to CORS, etc.)         }         else {             // something weird is happening         }     } //end snippet 
like image 189
ToddJCrane Avatar answered Oct 07 '22 03:10

ToddJCrane