jquery - Map Json data to an object using AJAX and C# -
how can use json formatted data sent ajax request in c#?
here's view jquery , ajax
<script type="text/javascript"> $(function () { $("#btnget").click(function () { var values = { "name": "manuel andrade", "dateofbirth": "12/01/1995" } $.ajax({ type: "post", url: "/api/webapi/getage", data: values, contenttype: "application/json; charset=utf-8", datatype: "json", success: function (response) { alert("hello: " + response.name + ".\ncurrent date , time: " + response.datetime); }, failure: function (response) { alert(response.responsetext); }, error: function (response) { alert(response.responsetext); } }); }); }); </script>
here's controller:
public class personcontroller : apicontroller { [system.web.http.route("api/webapi/getage")] [system.web.http.httppost] public person getage(person person) { //person = new person(); //person.name = "luis"; jsontest(person); //int ageinmonths = calculateageinmonths(person); //int ageinyears = calculateageinyears(person); //system.diagnostics.debug.writeline(ageinmonths); //system.diagnostics.debug.writeline(ageinyears); return person; } }
the person model has same attributes json variable in view. shouldn't create object automatically? method jsontest() works properly, used serialize data. if uncomment person instantiation , assignation luis method return json data view. if try use person parameter throws null exception. how can pass var values in view getage method can assign object?
it worked fine me when removed contenttype: "application/json; charset=utf-8",
line.
it isn't returned application/json default.
Comments
Post a Comment