0
我刚开始学习如何在asp.net中的正常web表单中使用jquery ajax。但是我卡住了。我不知道究竟是什么问题。 当我单击窗体上的按钮时,它不会在第一次运行,但有时它只在第二次单击时才有效。 然后停止工作(不会在成功功能中显示警告消息)。即使重建项目,它也不起作用。控制台中也不会显示错误。请帮帮我。JQuery Ajax函数调用问题
下面是我的代码 Employee类
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; }
}
Department类
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();
}
请让我知道我要去哪里错了。 任何帮助将非常感激。
谢谢你试图帮助我。但是我在Route.Config文件中没有这样的行。我甚至创建了一个空的项目,并尝试相同的,但它仍然不工作 – Videl