HTML code below is a simplified code taken from c-sharpcorner. I would like to modify this and transfer all logic in code behind. What I would like to know is how to convert the function, AddEmp, if I transfer this to btnSave's Click method. This is my first time using REST API and still learning to use it. Any advise is greatly appreciated.
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>