第二個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;
}
您是否獲得了 「現在保存」 警報,即使它沒有被呼叫?你有沒有在瀏覽器控制檯中的任何錯誤? – Rhumborl
不。控制檯中沒有錯誤。我也沒有收到任何警報。 – Karts
基本上,你可以將兩種方法合併爲一個.. –