Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<html>
<head>
    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#btnAdd").click(function () {

                var PersonalDetails = {
                    "id": $("#ID").val(),
                    "name": $("#Name").val(),
                    "adres": $("#Adress").val(),
                }
                $.ajax({
                    type: "POST",
                    url: 'http://localhost:54516/api/Values/PostPersonalDetails',
                    data: JSON.stringify(PersonalDetails),
                    contentType: "application/json;charset=utf-8",
                    processData: true,
                    success: function (data, status, xhr) {
                        alert("The result is : " + status);
                    },
                    error: function (xhr) {
                        alert(xhr.responseText);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <h2>Create</h2>
    <div>
        <label>Id</label>
        @Html.TextBox("ID")
    </div>
    <div>
        <label>Name</label>
        @Html.TextBox("Name")
    </div>
    <div>
        <label>Age</label>
        @Html.TextBox("Adress")
    </div>
    <div>
        <button id="btnAdd">Add</button>
    </div>
</body>
</html>


API CONTROLLER CODE

public class ValuesController1 : ApiController
   {

       [HttpPost]
       public IHttpActionResult PostPersonalDetails([FromBody]PersonalDetails personaldetails)
       {
           if (!ModelState.IsValid)
           {
               return BadRequest(ModelState);
           }

           SqlConnection co = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["con"].ConnectionString);
               co.Open();
                SqlCommand cmd = new SqlCommand("insert into udata values('" +personaldetails.id + "','" +personaldetails.name+ "','" +personaldetails.adres+ "')", co);
                cmd.ExecuteNonQuery();
                co.Close();
           return Ok(personaldetails);
       }
   }


What I have tried:

i used url:
'/api/Values/PostPersonalDetails'
Posted
Updated 14-Nov-17 23:41pm
v2
Comments
ZurdoDev 14-Nov-17 10:05am    
And what happens?
[no name] 14-Nov-17 22:52pm    
its showing 404 error.the url is not working.
F-ES Sitecore 15-Nov-17 5:43am    
It's not a valid url then. We have no access to your system so how can we tell you why it isn't valid? Rather than construct urls the way you are doing use @Url.Action instead if the url being accessed is in the same site.
ZurdoDev 15-Nov-17 7:22am    
404 means the url is not found. You need to provide the correct url.

1 solution

JSON.stringify(PersonalDetails) -> It convert JSON Object to string.
So you pass a string with the ajax call parameter acording the parameter
you should define the string .

Again in service
create a class name with the same fields of json array objects
List<class name> Meddata;
JavaScriptSerializer jsspms = new JavaScriptSerializer();
Meddata = jsspms.Deserialize<List<class name>>(personaldetails);

you can retrive the values using foreach loop
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900