使用 Ajax 调用进行验证不起作用

Validation using Ajax call not working

本文关键字:验证 不起作用 Ajax 调用 使用      更新时间:2023-09-26

我是Ajax和Jquery的新手,我有一个表单,其中有一个DepositAccountNumberId文本框,其值存储在隐藏字段中以进行验证。

OnBlur DepositAccountNumberId 文本框的事件应该给出一个引导框alert ("This Account Number has been Suspended")。我已经发布了下面的代码:

Javascript 函数来检查帐户暂停()

var exist = true;
    function checkAccountSuspension() {
        var accountNumberId = $('#DepositAccountNumberIdHiddenField').val();
      //  alert(accountNumberId);
        if (accountNumberId == "") {
            //
        } else {
            try {
                var url = '/WebMethods/AccountDetails.asmx/IsAccountSuspended';
               
                var d = { accountNumberId: accountNumberId };
                //var jqXhr = $.post(url, d);
                //jqXhr.done(function(data) {
                $.post(url, d, function (data) {
                    if (data) {
                        var ret = data.d;
                        if (ret) {
                            $('#DepositAccountNumberIdHiddenField').val(accountNumberId);
                            exist = true;
                        } else {
                            $('#DepositAccountNumberIdHiddenField').val('');
                            bootbox.alert("This Account Has been Suspended");
                            exist = false;
                        }
                    }
                }).fail(function() {
                    $('#DepositAccountNumberIdHiddenField').val('');
                });
            } catch (e) {
                bootbox.alert('Error: ' + e);
            }
        }

网络方法

  [WebMethod(EnableSession = true)]
    public bool IsAccountSuspended(string accountNumberId)
    {
        int officeId = OfficeId;
        return BusinessLayer.Transactions.Transactions.IsAccountSuspended(officeId, accountNumberId.ToLong());
    }

IS帐户在业务层中挂起

public static bool IsAccountSuspended(int officeId, long accountNumberId)
    {
        if (accountNumberId <= 0)
        {
            return false;
        }
        return DatabaseLayer.Transactions.Transactions.IsAccountSuspended(officeId,accountNumberId);
    }

Is帐户在数据库层中挂起

public static bool IsAccountSuspended(int officeId, long accountNumberId)
    {
        if (accountNumberId <= 0)
        {
            return false;
        }
        var sql = "SELECT * FROM deposit.is_suspended(@AccountNumberId::bigint);";
        using (var command = new NpgsqlCommand(sql))
        {
            command.Parameters.AddWithValue("@AccountNumberId", accountNumberId);
            using (var table = DBOperations.GetDataTable(command))
            {
                if (table.Rows.Count >= 1)
                {
                    return true;
                }
                return false;
            }
        }

    }

验证不起作用。不会调用 ajax 来检查帐户是否已暂停。请帮忙。

尝试使用$.post方法:

var exist = true;
function checkAccountSuspension() {
    var accountNumberId = $('#DepositAccountNumberIdHiddenField').val();
    //  alert(accountNumberId);
    if (accountNumberId == "") {
        //
    } else {
        try {
            var url = '/WebMethods/AccountDetails.asmx/IsAccountSuspended';
            var d = {accountNumberId: accountNumberId};
            var jqXhr = $.post(url, d);
            jqXhr.done(function (data) {
                if (data) {
                    var ret = data.d;
                    if (ret) {
                        $('#DepositAccountNumberIdHiddenField').val(accountNumberId);
                        exist = true;
                    } else {
                        $('#DepositAccountNumberIdHiddenField').val('');
                        bootbox.alert("This Account Has been Suspended");
                        exist = false;
                    }
                }
            }).fail(function () {
                $('#DepositAccountNumberIdHiddenField').val('');
            });
        } catch (e) {
            bootbox.alert('Error: ' + e);
        }
    }
}

$('#DepositAccountNumberTextBox').on('blur', function () {
    checkAccountSuspension();
});

在JQuery中没有像ajaxPost这样的方法。使用 $。在其位置发布。

try {
       var url = '/WebMethods/AccountDetails.asmx/IsAccountSuspended';
       var d = { accountNumberId: accountNumberId };
       $.post(url,d,function(data){
          if (data) {
             var ret = data.d;
             if (ret) {
                $('#DepositAccountNumberIdHiddenField').val(accountNumberId);
                exist = true;
             }
             else {
                $('#DepositAccountNumberIdHiddenField').val('');
                bootbox.alert("This Account Has been Suspended");
                exist = false;
             }
          }
      }).error(function() { 
          $('#DepositAccountNumberIdHiddenField').val('');
      })
    } 
    catch (e) {
        bootbox.alert('Error: ' + e);
    }

这就是它的工作原理。

就像上面的一些专家所说的使用postAjax和$.post一样。与项目中的任何地方一样,它被以前的开发人员用作postAjax。我实际上在隐藏字段中再次传递了一个空值。这段代码有效。

  var exist = true;
        function checkAccountSuspension() {
            var accountNumberId = $('#DepositAccountNumberIdHiddenField').val();
            if (accountNumberId == "") {
                //
            } else {
                try {
                    var url = '/WebMethods/AccountDetails.asmx/IsAccountSuspended';
                    var d = { accountNumberId: accountNumberId };
                    //$.post(url, d, function (data) {
                    var jqXhr = ajaxPost(url, d, true);
                    jqXhr.done(function (data) {
                        var ret = data.d;
                        if (!ret) {
                            $('#DepositAccountNumberIdHiddenField').val(accountNumberId);
                            exist = true;
                        }
                        else {
                            //$('#DepositAccountNumberIdHiddenField').val('');
                            //  bootbox.alert("<b class='text-red'>Suspended Account</b> <br/>This Account has been Suspended.");
                            swal({
                                title: "Suspended Account!",
                                text: "This Account is Suspended",
                                type: "error",
                                confirmButtonText: "OK",
                                imageSize: "20x20"
                            });
                            exist = false;
                            resetAllInputs();
                        }
                    }).fail(function (ex) {
                        bootbox.alert("Requested process fail to execute");
                        //$('#DepositAccountNumberIdHiddenField').val('');
                    });
                }
                catch (e) {
                    bootbox.alert('Error: ' + e);
                }
            }
        }