Reputation: 3104
Can anyone give me example of sending json data from jquery ajax to web api controller, both client and server code? For instance I want to send {Name: "SomeName", Email: "SomeEmail"}
through ajax as post request and to get those values at controller...
Upvotes: 0
Views: 926
Reputation: 23680
Server side to retrieve values:
public class RequestModel()
{
public string Name { get; set; }
public string Email { get; set; }
}
public MyWebApiController : ApiController
{
public object Post(RequestModel model)
{
// Do something
// Return same values back
return model;
}
}
Client side to post values:
$.ajax({
type: 'POST',
dataType: 'json',
url: '/Api/MyWebApi',
data: { Name = "Bob", Email = "[email protected]" },
success: function (responseData) {
// Do something on success, with the returned data
alert("Email:" + responseData.Email + ", Name:" + responseData.Name);
},
error: function (jqXHR, textStatus, errorThrown) {
// Display error?
}
})
Upvotes: 1