Passing multiple Json objects as data using jQuery $ .ajax ()

I am sending data to an MVC controller and I am trying to maintain state for an optimistic concurrency. I am currently sending a JSON request, but will be open to workable alternatives?

I am already sending the 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 and timestamps that I want to pass so that the server can decrypt it and then verify that the data is still fresh before it saves it.

It is very important that the data object is separate and not a descendant of one or the other or in the wrapper array (due to reflexive deserialization on the server). It is also important to note that I want to do this asynchronously and not how to submit the form.

My question is: is there a way to send two JSON objects using 'application / json' as the content type?

My other question is: is there a better / different way that I could do this?

early!

UPDATE . I solved my problem by changing the contentType parameter to the default and instead sending the ajax string data as separate named parameters to querystring.

When you use contentType: 'application / json; charset = utf-8 ', this pushes the data into the request body, not the query string. My new mail $ .ajax () 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 in this type of data operations, and I hope that someone who is looking for this in the future can stumble on this.

thanks!

Dan

+7
json jquery ajax asp.net-mvc
source share
5 answers

As far as I know, there is no way to send two completely different JSON objects that are not children of the same parent JSON object, and jQuery decodes it for you using the .ajax() method. You could, I suppose, respond with two JSON objects as strings, and then use an external JSON library to evaluate strings in a Javascript Object.

Does this answer your question?

by email Oh: you're asking about publishing two separate JSON objects for your controller from jquery ..., right? My bad ... Yes, you can do it ... Just change this line:

 data: $.toJSON(data), 

in

 data: { json_1:$.toJSON(data_1), json_2:$.toJSON(data_2) }, 

Sorry for the confusion.

+15
source share

If I understand you correctly, functional conversation, you want to send

 object 1, object 2 

which is equivalent to sending

 [ object 1, object 2 ] 

The only difference is that the array in the former case is implicit, and in the latter it is explicit. It still exists anyway, but if you want to send multiple objects in JSON, you need to use an explicit approach.

So wrapping two data objects in an array is indeed an ideal choice, but if your code doesn’t support it, you will need an alternative. The only possible method I can do for this is to place both data objects inside another object, for example:

 var data = {}; data[0] = data1; data[1] = data2; 

And then you send data via an AJAX call.

I would say that the problem is not in your approach, but in the processing of JSON on the receiving side.

+1
source share

You can do it in a simple way in asp.net if you just want to send some data to webmethod

 data: '{userName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '",userName1: "' + $("#<%=TextBox1.ClientID%>")[0].value + '" }', 
+1
source share

I think you can do something like this:

 var data = {}; var object1 = {}; var object2 = {}; data.object1 = object1;<br/> data.object2 = object2; // serializer JSON2.stringify(data); 
0
source share

This is the answer after a very long time, but may be useful to you in any future link.

I suggest you read the https://stackoverflow.com/a/360816/ which is directly solving your problem.

0
source share

All Articles