Reputation:
POST form variables to simple parameters of a Web API method in ASP.NET MVC 4 Web API
$.ajax({
url: 'api/products',
type: 'POST',
data: { Id: 2012, Name: 'test', Category: 'My Category', Price: 99.5 },
dataType: 'json',
success: function (data) {
alert(data);
}
});
but its not working how to do this?
Upvotes: 4
Views: 5373
Reputation: 1077
Use below code it will work. Only change I have made is in data
parameter where I am doing JSON.stringify()
, faced with same issue quite a few months back. Basically it expects a string which can be parsed to JSON
.
$.ajax({
url: 'api/products',
type: 'POST',
data: JSON.stringify({ Id: 2012, Name: 'test', Category: 'My Category', Price: 99.5 }),
dataType: 'json',
success: function (data) {
alert(data);
}
});
Upvotes: 1