tags:

views:

36

answers:

1

This is a difficult question for me to Google, so here goes.

I've encountered a problem in Firefox (does not happen in IE) where my Javascript kicks off a call to a service, which may take a little time to load. If the user navigates to another page before the call finishes, the error method of the jQuery.ajax() object is called.

I am looking for a way to either:

  1. Not have the error thrown when the user browses to another page when the request hasn't finishes, OR
  2. Have the error method distinguish between "real" errors and this error.

My error method, not surprisingly, shows the user an error, and I do not want to do this when the error is caused by navigating to another page.

My simple test code involves the following Javascript and a service that sleeps for 20 seconds then returns a string.

<script type="text/javascript" language="javascript">
    $(document).ready(function ()
    {
        $('#testLink').click(function ()
        {
            $.ajax({
                type: "POST",
                url: "/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data, textStatus, xhr)
                {
                    console.log(data);
                    console.log(textStatus);
                    console.log(xhr);
                },
                error: function (xhr, textStatus, errorThrown)
                {
                    console.log(xhr);
                    console.log(textStatus);
                    console.log(errorThrown);
                }
            });
        });
    });
</script> 
+1  A: 

Try:

<script type="text/javascript" language="javascript">
    $(function() {
        $('#testLink').click(function() {
            $.ajax({
                type: "POST",
                url: "/Services/NU.Web.NUnet.Services.UserService.asmx/LongRunningService",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data, textStatus, xhr){
                    console.log(data);
                    console.log(textStatus);
                    console.log(xhr);
                },
                error: function (xhr, textStatus, errorThrown) {
                    if (xhr.status === 500) {
                        console.log(xhr);
                        console.log(textStatus);
                        console.log(errorThrown);
                    }
                }
            });
        });
    });
</script> 
David Murdoch
Awesome, thanks a lot! The status is 0 when browsing to another page, so I'm checking against that value. This was a tricky one, because I couldn't see anything in Firebug because the page was reloading :x
wsanville
Your welcome. You can use the "Persist" button in Firebug's console for these types of errors.
David Murdoch