jQuery Validation插件submitHandler Ajax

时间:2016-05-07 12:07:34

标签: javascript jquery asp.net ajax

我在ASP.NET MVC中使用jQuery验证插件,我想使用$ .Ajax()来提交表单,但实际发生的事情实际上是我写的所有内容都被忽略了指向执行Ajax调用。这是我的代码。

submitHandler: function (form) {
    if ($(form).attr('id') == 'loginform') {

        var email = $('#email').val();
        var password = $('#password').val();
        var rememberme = $('#rememberme').is(':checked');

        $.ajax({  //When execution comes at this point in the browser, it throws an error of HTTP 500 and the entire Ajax call get skipped.
            url: '/Account/Login',
            type: 'POST',
            data: {
                email: email,
                password: password
            },
            cache: false,
            success: function () {
                window.location.href = "/Home/Index";
            },
            error: function () {
                // Fail message
                $('#success').html("<div class='alert alert-danger font-light-text'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-danger').append("Please provide correct administrative credentials!");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#loginForm').trigger("reset");
            },
        });
    }
    else if ($(form).attr('id') == 'registerform')
    {

    }
}

1 个答案:

答案 0 :(得分:0)

实际上我在我的行动中使用 ValidateAntiForgeryToken 。必须明确配置Ajax调用以进行验证。删除它,解决了我的问题。