jQuery > Ajax methods

.ajaxError() - Knowing if error occurred in ajax request in jQuery

How to know if an error occurred while performing ajax operation in jQuery?


To know if an error occurred while performing ajax operation, ajaxError() method can be used. It fires when ajax request completes with an error.

   <script>
     $(document).ajaxError(function (event, xhr, option, ex) {
       $("#divAjaxError").text("Error occurred while sending ajax request " + xhr.responseText);
     });
    </script>

In the above code snippet, when ajax request completes with error “Error occurred while sending ajax request” is written inside “divAjaxError”. After this error message, it also writes the response text returned from the server.

This event can be used to show error or setting the default value in case of error.

Important

This event only fires if the ajax request is not successful.

 Views: 5322 | Post Order: 117



Write for us






Hosting Recommendations