簡體   English   中英

使用Ajax調用進行驗證不起作用

[英]Validation using Ajax call not working

我是Ajax和Jquery的新手,我有一個表單,其中有一個DepositAccountNumberId文本框,其值存儲在一個用於驗證的隱藏字段中。

DepositAccountNumberId TextBox的OnBlur事件應發出引導框alert ("This Account Number has been Suspended") 我已經發布了以下代碼:

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

網絡方法

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

IsAccountSuspended在業務層中

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

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


    }

驗證不起作用。不會調用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這樣的方法。 在其位置使用$ .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);
    }

這就是它的工作方式。

就像上面的一些專家所說,有關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); } } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM