I am POSTing data to an MVC controller and I'm attempting to maintain state as well for optimistic concurrency. I'm currently posting back a JSON request, but would be open to workable alternatives?
I am already posting a name/value collection with the following command:
$.ajax({
url: g_appPath + "/Rounding.aspx/Round/" + $("#OfferId").val(),
type: 'POST',
dataType: 'html',
data: $.toJSON(data), // <-- data = name/value array
contentType: 'application/json; charset=utf-8',
beforeSend: doSubmitBeforeSend,
complete: doSubmitComplete,
success: doSubmitSuccess
});
I also have an (encrypted) array of id's and timestamps that I want to pass back so the server can decrypt it, then validate that data is still fresh before it saves it.
It is very important that the data object are separate and are not a child of one or the other or in a wrapper array (because of reflective deserialization at the server end). It is also important to note that I want to do this asynchronously and not as a form submit.
My question is: Is there any way I can post back 2 JSON objects using 'application/json' as the content type?
My other question is: Is there a better / another way I could be doing this?
thanks in advance!
UPDATE: I solved my problem, by changing the contentType parameter to default and instead sending the stringified ajax data as separate named parameters in the querystring.
When you use contentType: 'application/json; charset=utf-8', this pushes the data into the body of the request, rather than the querystring. My new $.ajax() post now looks like this:
$.ajax({
url: g_appPath + "/Rounding.aspx/Round/" + $("#OfferId").val(),
type: 'POST',
dataType: 'html',
data: "RoundingData=" + $.toJSON(data) + "&StateData=" + $.toJSON(stateData),
// --removed! contentType: 'application/json; charset=utf-8',
beforeSend: doSubmitBeforeSend,
complete: doSubmitComplete,
success: doSubmitSuccess
});
This question really arose because of my inexperience with this type of data operation, and I hope someone looking for this in the future might stumble across this.
thanks!
Dan