6

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 connectivity to the internet the error is called but I don't see anything in the responseText.

Is there a way I can find out different kind of errors based on status information in the returned ajaxContent? I would really like to be able to put out a message saying "Internet connectivity lost" and another message if there is some other problem.

Alan2
  • 23,493
  • 79
  • 256
  • 450

4 Answers4

2

According to the jQuery docu the error function receives three arguments:

  1. jqXHR:
  2. textStatus: a string describing the type of error that occurred
  3. errorThrown: an optional exception object, if one occurred

Furthermore it states:

Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

So you might want to have a look at the content of the second adn third parameter.

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Do I need all three parameters? Seems like they are also sent across if I just user ajaxContext. In that I get ajaxContext.readyState, ajaxContext.Status and ajaxContext.StatusText – Alan2 Jun 07 '12 at 11:19
  • @Gemma In JS you don't need to capture all arguments of a function. Only the order of the arguments is important. So, if you specify two parameters in your function header, only those will be filled and the third one will be dropped. (There are means to access all arguments of a function regardless of how many were defined, though, but they are not needed here) – Sirko Jun 07 '12 at 11:25
  • @Sirko is almost right: look at 2nd argument. If null, you **can** tell for sure **there was NO network connectivity problem**, and 3rd argument is the HTTP status sent back by server. If not null, I don't think you will be able to tell for sure: "timeout" and "error" are **clues for network connectivity down**, but are no definitive proof (could be network lag or even server lag). "parsererror" is clue for a response somehow received, therefore a rather operational network. But response might have been corrupted because of network issues as well… And "abort" could be because of user. Or not. – Alain BECKER Jun 07 '12 at 11:53
1

Updated.

You should just add: timeout: , 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.

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

Community
  • 1
  • 1
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
  • I strongly disagree, because a status is an HTTP response received **from the server**, so getting one (be it 404 or any other) would be a proof network connectivity is OK. – Alain BECKER Jun 07 '12 at 11:35
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)
    }
});
Raab
  • 34,778
  • 4
  • 50
  • 65
  • I see some people are saying there are three parameters. However if I use ajaxContext then can I still access all three ? – Alan2 Jun 07 '12 at 11:17
  • I am trying to understand if these three are different for the jqXHDR, textStatus and errorThrown. – Alan2 Jun 07 '12 at 11:26
  • 1
    @CoDeaDDict : I strongly disagree, because a status is an HTTP response received from the server, so getting one (be it 404 or any other) would be a proof network connectivity is OK. See Sirko's answer and my comments. – Alain BECKER Jun 07 '12 at 12:02
0

if you mean defferent kinds of response results it's here - http://api.jquery.com/jQuery.ajax/ - statusCode parameters

shershen
  • 9,875
  • 11
  • 39
  • 60