有成功的失败部分吗?

时间:2018-04-23 02:23:16

标签: jquery ajax

我注意到我可以在.ajax中添加成功部分。当我使用jQuery时,有没有办法添加失败部分?

$('#signUpSubmit').click(function() 
{
    //alert("signup completed");
    var email=document.getElementById('signUpEmail').value;
    var password = document.getElementById('signUpPassword').value;
    $.ajax({
        url: 'signup.php',
        type: 'POST',
        data: {
            email: email,
            password: password
        },
        success: function() {
            alert('Email Sent');
        }               
    });
});

修改
我尝试使用以下但是我的提交按钮没有激活,虽然密码相同

col-form-label">Password:</label>
                                            <input type="password" class="form-control" id="signUpPassword" name="password" onchange="check_pass()">
                                        </div>
                                        <div class="form-group">
                                            <label for="pwd" class="col-form-label">Confirm Password:</label>
                                            <input type="password" class="form-control" id="signUpConPassword" name="password" onchange="check_pass()">
                                        </div>
                                    </form>
                                </div>
                                <div class="modal-footer">
                                    <button type="submit" class="btn btn-primary" id="signUpSubmit" disabled="true" >Sign Up</button>

                                </div>
                            </div>
                        </div>
                    </div>

<script type="text/javascript">
    function check_pass() 
    {
        //alert(document.getElementById('signUpPassword').value);
    if (document.getElementById('signUpPassword').value ==
            document.getElementById('signUpConPassword').value) {
        document.getElementById('signUpSubmit').disabled = false;
    }
    else
    {
        document.getElementById('signUpSubmit').disabled = true;
    }
}
$('#signUpSubmit').click(function() 
{
    //alert("signup completed");
    var email=document.getElementById('signUpEmail').value;
    var password = document.getElementById('signUpPassword').value;
    $.ajax({
        url: 'signup.php',
        type: 'POST',
        data: {
            email: email,
            password: password
        },
        success: function() {
            alert('Email Sent');
        }      
        error: function()
        {
            alert('something is wrong');
        }         
    });
});
</script>

3 个答案:

答案 0 :(得分:0)

尝试“error:function(){...}”

答案 1 :(得分:0)

有一个错误部分,可以捕获错误请求或其他失败案例,请尝试

error: function() {
        alert('this is error');
    } 

此外,如果您使用完成:,它将同时包含成功和错误情况。在发生错误或错误后完成执行。

答案 2 :(得分:0)

$.ajax({
    url: 'signup.php',
    type: 'POST',
    data: {
        email: email,
        password: password
    },
    success: function() {
        alert('Email Sent');
    }   
}).fail(function(jqXHR, textStatus) {
    alert( "Request failed: " + textStatus );
}); 

您可以在ajax之后尝试.fail,它也提供错误代码,例如404,500。