I am working through a new project using Knockout.js and the ASP.NET web ApiController. Many of the examples I see perform some manual JSON serialization before posting the data to the server. Additionally, the request content type is equally often set to "application/json".
I am wondering why this is necessary. I'm assuming there's something I haven't yet encountered that makes this either required or at least preferable.
Currently, I am having no issues sending any data I desire to the server using these jQuery ajax options:
cache: false,
traditional: true,
type: 'POST',
Here's sample JS object and corresponding server-side C# model object that POSTs and binds to the ApiController action method just fine.
//JS object
var requestDataObject = {
accountId: vm.accountId,
range: [1, "a'b\"c", 3],
start: new Date(2012, 12, 12)
};
//C# model object
public class RequestData
{
public int AccountId { get; set; }
public string[] Range { get; set; }
public DateTime Start { get; set; }
}
//Action method signature
[HttpPost]
public HttpResponseMessage GetAccountUsage(RequestData requestData){
...
What am I missing?
POSTcan only send simple name-value pairs -- everything looks like a string on arrival at the server. Using JSON lets you send more complicated objects.JSON.stringify?