Open In App

jQuery deferred.catch() Method

Last Updated : 22 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The deferred.catch() method in jQuery is used to add handlers which are to be called when the deferred object is rejected.

Syntax:

deferred.catch(failedFilter)

Parameters:

  • failedFilter: This parameter specifies a function which is to be called when the deferred object is rejected.

Return Value:

  • This method returns the deferred object.

Example 1:

html
<!DOCTYPE HTML>
<html>

<head>
    <script src=
"https://code.jquery.com/jquery-3.5.0.js">
    </script>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>

    <p>
        JQuery | deferred.catch() method
    </p>
    
    <button onclick="Geeks();">
        click here
    </button>
    
    <script>
        function Geeks() {
            $.get("testingGFG.php")
                .then(function () {
                    alert(
            "$.get successfully completed!");
                })
                .catch(function () {
                    alert("$.get failed!");
                });
        } 
    </script>
</body>

</html>

Output:

Before clicking on button:

After clicking on button:

Example 2:

html
<!DOCTYPE HTML>
<html>

<head>
    <script src=
"https://code.jquery.com/jquery-3.5.0.js">
    </script>
</head>

<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>

    <p>
        JQuery | deferred.catch() method
    </p>
    
    <button onclick="Geeks();">
        click here
    </button>
    
    <p id="GFG_DOWN"></p>

    <script>
        var el_down = document
                .getElementById("GFG_DOWN");
        function Geeks() {
            $.get("testingGFG.php")
                .then(function () {
                    el_down.innerHTML = 
                        "$.get successfully completed";
                })
                .catch(function () {
                    el_down.innerHTML = "$.get failed!";
                });
        } 
    </script>
</body>

</html> 

Output:


Next Article

Similar Reads