How to disable some jQuery global Ajax event handlers for a request?

Use the global error handler to receive a few of the parameters that jQuery can provide. After that add the suppressErrors: true option to your AJAX request configuration. This approach allows you to selectively disable global AJAX event handlers for specific requests while keeping them active for others.

The suppressErrors property is a custom flag that you can check within your global error handler. When this flag is set to true, the handler can return early without executing its error handling logic.

Example

You can try to run the following code to disable some jQuery global Ajax event handlers for a request ?

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $("div.log").ajaxError(function(evt, xhr, settings) {
                if(settings.suppressErrors) {
                    return;
                }
                $(this).text("ajaxError handler is triggered");
            });
            
            $("button.trigger").click(function() {
                $("div.log").text('');
                $.ajax("ajax/missing.html");
            });

            $("button.triggerSuppress").click(function() {
                $("div.log").text('');
                $.ajax("ajax/missing.html", {
                    suppressErrors: true
                });
            });
        });
    </script>
</head>
<body>
    <button class="trigger">Trigger (process errors)</button>
    <button class="triggerSuppress">Trigger (won't process errors)</button>
    <p>Check log</p>
    <div class="result"></div>
    <div class="log"></div>
</body>
</html>

In this example, the first button triggers an AJAX request that will activate the global error handler when the requested file is not found. The second button makes the same request but with suppressErrors: true, which prevents the global error handler from displaying the error message.

Conclusion

By using custom properties like suppressErrors in your AJAX settings, you can selectively control which requests should trigger global event handlers, providing fine-grained control over error handling behavior.

Updated on: 2026-03-13T19:22:46+05:30

618 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements