Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Ajax call failure due to network disconnected

Tags:

I am sending lots of data using jquery ajax method to web sever and client side respond only after receiving acknowledgment from server, now suppose network connection lost in MIDDLE of ajax call then how to detect this situation.

$.ajax({
         url:'server.php',
         data:'lots of data from 200KB to 5MB',
         type:'post',
         success: function(data)
                    {
                        alert('Success');
                      //some stuff on success

                    },
          error: function(XMLHttpRequest, textStatus, errorThrown)
                    {
                        alert('Failure');
                      //some stuff on failure
                    }
        });

This is my code and and it does not give error in middle of ajax call if get internet is disconnected.

NOTE : I cant use time out because data size is vary from 200kb to 5MB and server response time calculation is not feasible.

like image 507
Ritesh Chandora Avatar asked Jun 15 '12 04:06

Ritesh Chandora


People also ask

How do you check if AJAX call is completed?

The ajaxStop() method specifies a function to run when ALL AJAX requests have completed. When an AJAX request completes, jQuery checks if there are any more AJAX requests. The function specified with the ajaxStop() method will run if no other requests are pending.

Why does AJAX call fail?

ajax method lets you set a timeout in milli seconds. When a timeout happens, The fail callback is called, with errorThrown set to "timeout". The request is aborted, meaning that even if the response arrives later on, your done callback is not called by jQuery.

What is the meaning of 404 status code in AJAX?

404 - Not Found. The URL requested was not found on the server. Check for typos in the file name in the ajax parameter and in your file on the server.

What triggers AJAX success?

AJAX success is a global event. Global events are triggered on the document to call any handlers who may be listening. The ajaxSuccess event is only called if the request is successful. It is essentially a type function that's called when a request proceeds.


1 Answers

Try this:

First create a "ping" ajax call with setInterval every 5 seconds

function server_ping()
    {
        $.ajax({
            url:"url to ping",
            type: "POST"
        });
    }
    var validateSession = setInterval(server_ping, 5000);

then arm your .ajaxError trap:

$(document).ajaxError(function( event, request, settings ) {
        //When XHR Status code is 0 there is no connection with the server
        if (request.status == 0){ 
            alert("Communication with the server is lost!");
        } 

    });

Remember Ajax calls are Asynchronous by default, so when the pings are going to the server and the request cannot reach the server the value on the XHR status is 0, and the .ajaxError will fire and you must catch the error and handle the way you want it.

Then you can send your data to the server, if the connection is lost when sending the data you get the error reported by the ping.

like image 70
DariusVE Avatar answered Sep 22 '22 19:09

DariusVE