在jquery中的另一个ajax调用中的ajax调用不会间歇性地触发

时间:2016-06-15 08:18:22

标签: jquery asp.net ajax

第二次ajax调用中的webmethod(SaveSalaryDetails)没有被间歇性地调用。如果我正在调试javascript / jquery,它会被触发。以下是代码:

    function fnCheckAndSaveSalary() {
    debugger;
    var salary = document.getElementById("   <%=txtSelEmployeeSalary.ClientID %>").value;
    var employeeID = $find("<%=rcmbEmployees.ClientID %>").get_value();
    var employeeName = $find("<%=rcmbEmployees.ClientID %>").get_text();
    var paidDate = $find("<%=radPayDate.ClientID %>").get_selectedDate();
    //var lblStatus = document.getElementById("<%=lblStatus.ClientID %>");
    //setTimeout(saveSalary, 15000);
    $.ajax({
        type: 'POST',
        contentType: 'application/json;charset=utf-8',
        datatype: 'json',
        data: JSON.stringify({ EmpID: employeeID, PaidDate: paidDate }),
        url: 'Pay.aspx/IsSalaryExisting',
        success: function(response) {
            debugger;
            alert("Checked for records" + response.d);
            //debugger;
            //No record exists for the particular month and year for the employee
            if (response.d == false) {
                debugger;
                alert("Now saving..");
                saveSalary(salary, employeeID, paidDate);
            }
            else {
                //debugger;
                alert('Salary already exists for Employee:' + employeeName + ' for date:' + paidDate);
            }
        },
        failure: function(response) {
            alert(response.d);
        }
    });
}

function saveSalary(salary, employeeID, paidDate) {
    $.ajax({
        type: 'POST',
        contentType: 'application/json;charset=utf-8',
        datatype: 'json',
        data: JSON.stringify({ Salary: salary, EmpID: employeeID, PaidDate: paidDate }),
        url: 'Pay.aspx/SaveSalaryDetails',
        success: function(response) {
            //debugger;
            alert('Successfully saved');
        },
        failure: function(response) {
            alert('Error in saving the salary details' + response.d);
        }
    });
}


<form id="form1" runat="server">
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<div>
<table>
<tr>
    <td>Select Employee:</td>
    <td><telerik:RadComboBox ID="rcmbEmployees" runat="server" OnSelectedIndexChanged="rcmbEmployees_SelectedIndexChanged" AutoPostBack="true"></telerik:RadComboBox>
    </td>
</tr>
<tr>
    <td>Salary: </td>
    <td><asp:TextBox ID="txtSelEmployeeSalary" runat="server"></asp:TextBox>
    </td>
</tr>
<tr>
    <td>Pay Date:</td>
    <td><telerik:RadMonthYearPicker ID="radPayDate" runat="server"  
    MinDate="1/1/2000" MaxDate="1/1/3000"></telerik:RadMonthYearPicker></td>
</tr>
</table> 
<asp:Button ID="btnSave" runat="server" Text="Save" OnClientClick="fnCheckAndSaveSalary()" />
<asp:Label ID="lblStatus" runat="server"></asp:Label>
</div>
</form>

代码隐藏文件:

    [WebMethod]
    public static int SaveSalaryDetails(string Salary, string EmpID, string PaidDate)
    {
        //saves details in the database and returns ID

        }
        catch (Exception ex)
        {
            throw ex;
        }
        return objEmpSalaryObj.PaymentID;
    }
    [WebMethod]
    public static bool IsSalaryExisting(string EmpID, string PaidDate)
    {
        EmployeeController objController = new EmployeeController();
        EmployeePayHistoryVo objEmpSalaryObj = new EmployeePayHistoryVo();
        bool salaryExists = false;
        try
        {
            objEmpSalaryObj.EmployeeID = Convert.ToInt32(EmpID);
            objEmpSalaryObj.PaidMonth = (Convert.ToDateTime(PaidDate)).ToString("MMMM");
            objEmpSalaryObj.PaidYear = (Convert.ToDateTime(PaidDate)).Year;

            salaryExists = objController.IsSalaryExisting(objEmpSalaryObj);
            //System.Threading.Thread.Sleep(1000);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return salaryExists;
    }

1 个答案:

答案 0 :(得分:0)

后端

   [WebMethod]
    public static int IsSalaryExisting(string EmpID, string PaidDate, string Salary)
    {

        EmployeeController objController = new EmployeeController();
        EmployeePayHistoryVo objEmpSalaryObj = new EmployeePayHistoryVo();
        var salaryExists = 0;
        try
        {
            objEmpSalaryObj.EmployeeID = Convert.ToInt32(EmpID);
            objEmpSalaryObj.PaidMonth = (Convert.ToDateTime(PaidDate)).ToString("MMMM");
            objEmpSalaryObj.PaidYear = (Convert.ToDateTime(PaidDate)).Year;

            return !objController.IsSalaryExisting(objEmpSalaryObj) ? SaveSalaryDetails(Salary, EmpID, PaidDate) : 0;
            //System.Threading.Thread.Sleep(1000);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return salaryExists;
    }

你的JS代码

function fnCheckAndSaveSalary() {
    debugger;
    var salary = document.getElementById("   <%=txtSelEmployeeSalary.ClientID %>").value;
    var employeeID = $find("<%=rcmbEmployees.ClientID %>").get_value();
    var employeeName = $find("<%=rcmbEmployees.ClientID %>").get_text();
    var paidDate = $find("<%=radPayDate.ClientID %>").get_selectedDate();   
    $.ajax({
        type: 'POST',
        contentType: 'application/json;charset=utf-8',
        datatype: 'json',
        data: JSON.stringify({ EmpID: employeeID, PaidDate: paidDate }),
        beforeSend: function(){
            alert("Now saving..");
        },
        url: 'Pay.aspx/IsSalaryExisting',
        success: function(response) {
            debugger;
            alert("Checked for records" + response.d);
            //debugger;
            //No record exists for the particular month and year for the employee
            if (response.d > 0) {
                debugger;
                alert('Successfully saved');                
            }
            else {
                //debugger;
                alert('Salary already exists for Employee:' + employeeName + ' for date:' + paidDate);
            }
        },
        failure: function(response) {
            alert(response.d);
        }
    });
}