0

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 &nbsp; <asp:TextBox runat="server" ID="txtFirstName" />  
        <br />
        Last Name &nbsp; <asp:TextBox runat="server" ID="txtLastName" />  
        <br />
        Company &nbsp; <asp:TextBox runat="server" ID="txtCompany" />  
        <br />
        <asp:Button Text="Save" runat="server" ID="btnSave" />  
    </form>  
</body>  
</html>  
  • 1
    Possible duplicate of [How do I make calls to a REST api using C#?](https://stackoverflow.com/questions/9620278/how-do-i-make-calls-to-a-rest-api-using-c) – Chetan Feb 13 '19 at 03:39
  • You need to read about How to call Web API from C#. Look the duplicate question or [here](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client) to learn more. – Chetan Feb 13 '19 at 03:40

0 Answers0