jQuery deferred.fail()方法
在jQuery的deferred.fail()方法是用来添加处理程序,当递延对象被拒绝时,将被调用。这个方法接受一个或一个以上的参数,可以是一个函数或一个数组的功能。回调是按照它们被添加的顺序执行的。当递延对象被拒绝,失败的回调被调用。
语法:
deferred.fail(failedCallbacks, failedCallbacks )
参数:
- failedCallbacks。这个参数指定一个函数,或一个函数数组,当递延被拒绝时将被调用。
 - failedCallbacks。这个参数指定了可选的附加函数,或一个函数数组,当递延被拒绝时将被调用。
 
返回值:该方法返回递延对象。
示例 1:
<!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.fail() method
    </p>
  
    <button onclick="Geeks();">
        click here
    </button>
      
    <script>
        function Geeks() {
            .get("testingGFG.php")
                .done(function () {
                    alert(".get successfully completed!");
                })
                .fail(function () {
                    alert("$.get failed!");
                });
        } 
    </script>
</body>
  
</html>
输出:
在点击按钮之前:

点击按钮后:

示例 2:
<!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.fail() 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")
                .done(function () {
                    el_down.innerHTML = 
                        ".get successfully completed";
                })
                .fail(function () {
                    el_down.innerHTML = "$.get failed!";
                });
        } 
    </script>
</body>
  
</html>
输出:

极客教程