Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

asp.net mvc - Passing Multiple Json Objects as data using jQuery's $.ajax()

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As far as I know, there is no way to send back two completely different JSON objects that aren't children of a single parent JSON object and have jQuery decode it for you using the .ajax() method. You could, I suppose, reply with two JSON objects as strings and then use an external JSON library to evaluate the strings into a Javascript Object.

Does that answer your question?

Ooops: You're asking about posting two distinct JSON objects to your controller from jquery..., right? My bad... Yeah, you can do that... Just change this line:

data: $.toJSON(data),

to:

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

Sorry about the mix-up.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...