Introduction:

Here we are going to explain what is ajaxError() method  or how to use ajaxError()  method or why use ajaxError() method  or   how to show message on the web page when error occurred while performing ajax request  in jQuery with example.

 Description: 

The jQuery ajaxError() method is used to track error when ajax request completes with an error.

Whenever an Ajax request completes with an error, jQuery triggers the ajaxError event. Any and all handlers that have been registered with the .ajaxError() method are executed at this time.

Let’s see a demonstration example of ajaxError() method 

<script src="/Scripts/jquery-3.2.1.js"></script>
 <script type="text/javascript">
       $(document).ready(function () {
            varserviceURL = '/Home/Get';             $.ajax({
                type: "Get",
                url: serviceURL,
                data: param = "",
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: successFunc              
            });
            function successFunc(data, status) {    
                alert(data);
            }
       });
               $(document).ajaxError(function(event, xhr, option, ex) {
            $("#divAjaxError").text("Error occurred while                 sending ajax request " + xhr.responseText);
       });
    </script>
 
<div id="divAjaxError">
    </div>

 

In the above example code, when ajax request completes with error “Error occurred while performing  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 default message in case of error.

I hope it will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.