JQuery
POST-ing JSON to an ASP.NET Web API controller
So you want to post some JSON to an ASP.NET Web API controller?
I bet you’ve written this more than you wanted to:
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(data),
dataType: "json",
contentType: "application/json; charset=utf-8"
});
I hate that work-around where you specify all sorts of stuff that you don’t really want to specify (like dataType
, contentType
, etc…). This is all required because the friendlier $.post(...)
will send your data in a Form encoded way.