简体   繁体   English

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

[英]Validation using Ajax call not working

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") . DepositAccountNumberId TextBox的OnBlur事件应发出引导框alert ("This Account Number has been Suspended") I have posted the code below: 我已经发布了以下代码:

Javascript Function to CheckAccountSuspension() 用于CheckAccountSuspension()的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); } } 

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来检查帐户是否被暂停。请帮助。

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

There is no such method like ajaxPost in JQuery. 在JQuery中没有像ajaxPost这样的方法。 Use $.Post at its place. 在其位置使用$ .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);
    }

This is How it worked. 这就是它的工作方式。

Like some of the experts above said about the use of postAjax and $.post. 就像上面的一些专家所说,有关postAjax和$ .post的使用。 As everywhere in the project it was used as postAjax by previous developers here. 在项目的各处,以前的开发人员都将其用作postAjax。 I was actually passing a null value again in the hidden field. 我实际上是在隐藏字段中再次传递了空值。 This code worked. 此代码有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM