2016-06-15 60 views
0

第二個ajax調用中的webmethod(SaveSalaryDetails)不會間歇性地調用。如果我正在調試javascript/jquery,它會被解僱。下面是代碼:ajax調用另一個在jQuery中的ajax調用不是間歇性觸發

隱藏文件
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; 
    } 
+0

您是否獲得了 「現在保存」 警報,即使它沒有被呼叫?你有沒有在瀏覽器控制檯中的任何錯誤? – Rhumborl

+0

不。控制檯中沒有錯誤。我也沒有收到任何警報。 – Karts

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

只需檢查響應是否具有屬性「d」,否則您可以在「響應」本身中得到結果 –

+0

謝謝,我按照您的建議更改了代碼。現在我只有一個ajaxcall。它在調用方法之前每次顯示「Nowsaving」警報。但在調用「IsSalaryExisting」方法後,成功部分中的警報消息(「檢查記錄」)仍有時不顯示。 – Karts

+0

嘗試使用引導模式對話框而不是警報。 –