[英]JQuery Ajax function call issue
我刚刚开始学习如何在asp.net中以普通Web形式使用jquery ajax。 但是我被困住了。 我不知道到底是什么问题。 当我单击表单上的按钮时,它第一次不起作用,但是有时它仅在第二次单击时才起作用。 然后停止工作(在成功函数中不显示警报消息)。即使我重建项目,也无法工作。 控制台中也不会显示任何错误。 请帮我。
下面是我的代码员工班
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string Country { get; set; }
public int Salary { get; set; }
public int DeptId { get; set; }
}
部门课
public class Department
{
public int DeptId { get; set; }
public string Name { get; set; }
}
test.aspx代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
$(document).ready(function () {
$('#Button1').click(function() {
var Name = $("#txtName").val();
var Gender = $("#ddlGender").val();
var Country = $("#txtCountry").val();
var Salary = $("#txtSalary").val();
var DeptId = $("#ddlDept").val();
var employee = {
"Name": Name,
"Gender": Gender,
"Country": Country,
"Salary": Salary,
"DeptId": DeptId
}
$.ajax({
context: this,
type: "POST",
url: "test.aspx/GetResultFromDB",
data: JSON.stringify({ employee: employee }),
contentType: "application/json; charset=utf-8",
dataType: "json",
cache:false,
success: OnSuccess,
failure: function (response) {
alert("In Error");
alert(response.d);
}
});
});
function OnSuccess(response) {
alert("In succcess");
alert(response.d);
console.log(response.d);
}
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<table>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Country</td>
<td><asp:TextBox ID="txtCountry" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Gender</td>
<td><asp:DropDownList ID="ddlGender" runat="server">
<asp:ListItem Text="Select" Value="0"></asp:ListItem>
<asp:ListItem Text="Male" Value="Male"></asp:ListItem>
<asp:ListItem Text="Female" Value="Female"></asp:ListItem>
</asp:DropDownList></td>
</tr>
<tr>
<td>Salary</td>
<td>
<asp:TextBox ID="txtSalary" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Department</td>
<td><asp:DropDownList ID="ddlDept" runat="server"></asp:DropDownList></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" Text="Button"/>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
</html>
文件后面代码中的代码
[System.Web.Services.WebMethod]
public static string GetResultFromDB(Employee employee)
{
SqlConnection con = EmpDeptDataLayer.GetDBConnection();
SqlCommand cmd = new SqlCommand("spAddEmployee", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Name", employee.Name));
cmd.Parameters.Add(new SqlParameter("@Country", employee.Country));
cmd.Parameters.Add(new SqlParameter("@Gender", employee.Gender));
cmd.Parameters.Add(new SqlParameter("@Salary", employee.Salary));
cmd.Parameters.Add(new SqlParameter("@DeptId", employee.DeptId));
cmd.Parameters.Add(new SqlParameter("@Result", SqlDbType.VarChar, 50)).Direction = ParameterDirection.Output;
con.Open();
int count = cmd.ExecuteNonQuery();
con.Close();
string Result = "";
if (count > 0)
{
Result = cmd.Parameters["@Result"].Value.ToString();
}
else
{
Result = "Error!! Something went wrong";
}
return Result.ToString();
}
请让我知道我要去哪里错了。 任何帮助将不胜感激。
代码很好,对我有用。
在您的App_Start文件夹中,在RouteConfig中...。
注释掉以下行或将其更改为RedirectMode:
//settings.AutoRedirectMode = RedirectMode.Permanent;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.