HTML代码是摘自c-sharpcorner的简化代码。我想对此进行修改,并在后面的代码中转移所有逻辑。我想知道如何将函数AddEmp转换为btnSave的Click方法。这是我第一次使用REST API,现在仍在学习使用它。任何建议都将不胜感激。
protected void btnSave_Click(object sender, EventArgs e)
{
}
HTML
<html>
<head runat="server">
<title>Code snippet taken from Vithal Wadje's article</title>
<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
function AddEmp() {
var Emp = {};
Emp.FirstName = $("#txtFirstName").val();
Emp.LastName = $("#txtLastName").val();
Emp.Company = $("#txtCompany").val();
$.ajax({
url:"<%=Page.ResolveUrl("/api/Emp/AddEmployees")%>",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify(Emp),
dataType: "json",
success: function (response) {
alert(response);
},
error: function (x, e) {
alert('Failed');
alert(x.responseText);
alert(x.status);
}
});
}
$(document).ready(function ()
{
$("#btnSave").click(function (e) {
AddEmp();
e.preventDefault();
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
First Name <asp:TextBox runat="server" ID="txtFirstName" />
<br />
Last Name <asp:TextBox runat="server" ID="txtLastName" />
<br />
Company <asp:TextBox runat="server" ID="txtCompany" />
<br />
<asp:Button Text="Save" runat="server" ID="btnSave" />
</form>
</body>
</html>