使用Ajax调用验证无法正常工作

时间:2022-04-26 01:25:51

I am new to Ajax and Jquery.I have a form where there is a DepositAccountNumberId text box and its value is stored in a Hidden Field for Validation.

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

OnBlur event of DepositAccountNumberId TextBox should give a bootbox alert ("This Account Number has been Suspended"). I have posted the code below:

DepositAccountNumberId的OnBlur事件TextBox应该给出一个引导箱警报(“此帐号已被暂停”)。我已经发布了以下代码:

Javascript Function to CheckAccountSuspension()

Javascript函数CheckAccountSuspension()

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);
            }
        }

Web Method

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

IsAccountSuspended in Business Layer

IsAccountSuspended在业务层

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

IsAccountSuspended in Database Layer

IsAccountSuspended在数据库层中

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;
            }
        }


    }

The Validation does not work.The ajax is not called to check if the account is suspended.Help Please.

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

3 个解决方案

#1


0  

Try to use $.post method:

尝试使用$ .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();
});

#2


0  

There is no such method like ajaxPost in JQuery. Use $.Post at its place.

在JQuery中没有像ajaxPost这样的方法。在它的位置使用$ .Post。

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);
    }

#3


0  

This is How it worked.

这是它的工作原理。

Like some of the experts above said about the use of postAjax and $.post. As everywhere in the project it was used as postAjax by previous developers here. I was actually passing a null value again in the hidden field. This code worked.

像上面的一些专家所说的关于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);
                }

            }
        }

#1


0  

Try to use $.post method:

尝试使用$ .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();
});

#2


0  

There is no such method like ajaxPost in JQuery. Use $.Post at its place.

在JQuery中没有像ajaxPost这样的方法。在它的位置使用$ .Post。

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);
    }

#3


0  

This is How it worked.

这是它的工作原理。

Like some of the experts above said about the use of postAjax and $.post. As everywhere in the project it was used as postAjax by previous developers here. I was actually passing a null value again in the hidden field. This code worked.

像上面的一些专家所说的关于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);
                }

            }
        }