How can I get information from an Ajax call when I lose my Internet connection?

I have the following:

 $.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        alert(ajaxContext.responseText)
    }
});

When I lose my Internet connection, an error is raised, but I do not see anything in the responseText.

Is there a way to find out the various errors based on the status information in the returned ajaxContent? I would really like for me to be able to post a message stating that "Internet connection is lost", and another message if there is any other problem.

+5
source share
4 answers

According to the jQuery document , the error function receives three arguments:

  • jqXHR:
  • textStatus: a string describing the type of error that occurred
  • errorThrown: optional exception object if occurred

In addition, he indicates:

( ) - "", "", "" "parsererror". HTTP errorThrown HTTP-, "Not Found" "Internal Server Error".

, .

+2

.

: timeout:, - $.ajax({}). , cache: false, .

$. ajax , , - .

JQuery Ajax - Ajax

+1
$.ajax({ cache: false,
    url: "/Admin/Contents/GetData",
    data: { accountID: AccountID },
    success: function (data) {
        $('#CityID').html(data);
    },
    error: function (ajaxContext) {
        if(ajaxContext.status=="404")
         {
          //write your not found handler code here
         }
        else
        alert(ajaxContext.status)
    }
});
+1

if you mean different kinds of answers, the results here are http://api.jquery.com/jQuery.ajax/ - statusCode parameters

0
source

All Articles