jQuery ajax error details

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #1

    jQuery ajax error details

    Hi there!

    I have a function that handles all of the form submit functions on a page that uses the jQuery ajax api to submit the request asynchronously and then update a particular <div> on the page.

    Here is my code:

    Code:
        function Refresh() {
            $(document.forms[0]).submit();
        }
    
        $(document).ready(function () {
            $(document).on("submit", "form", function (event) {
                event.preventDefault();
                var form = $(this);
                $.ajax({
                    type: this.method,
                    url: this.action,
                    data: form.serialize(),
                    cache: false,
                    success: function (result) {
                        var lastRefreshed = new Date();
                        var str = lastRefreshed.toString() + result;
                        $('#itemsContent').html(str);
                    },
                    complete: function () {
                        window.setTimeout(Refresh, 5000);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        var lastRefreshed = new Date();
                        var str = lastRefreshed.toString() +"<br /><br />"+errorThrown;
                        $('#itemsContent').html(str);
                    }
                });
    
            });
    
        });
    Currently, the items are refreshed every 5 seconds after a submit to the server and it works fine under normal conditions.

    However, when an error occurs, I want to display the error details to the user.

    To test this I stopped the server process completely to cause a "server unavailable" error.

    In the error function, the textStatus parameter contains text "error" which is not very informative. I would have expected the errorThrown to contain information about the error (in this case: server not available or the HTTP status code) but it contains nothing.


    Is there a way to display more information about the error to the user?

    How do I retrieve this information?

    Thanks,

    -Frinny
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Is there a way to display more information about the error to the user?
    hardly when you stop the server. (or asked the other way round, if the server does not respond, where should the error message come from?)

    that’s because the error messages normally come from the server itself (i.e. the 4xx and 5xx HTTP errors).

    the only workaround for a stopped server I can imagine is to hook into the timeout event (if there is any, well the regular XHR seems to have a timeout event (ref.)).

    for jQuery directly, you may find something in the ajaxError() description.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Ah, thanks Dorm :)

      I should have thought about the test I was doing.

      Going to try submitting to an invalid script to get a 404 error instead and test that way.

      -Frinny
      Last edited by Frinavale; Oct 23 '13, 06:29 PM.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Yup, that's better!
        It is working as expected.

        Thanks :)

        Comment

        Working...