传递使用多个JSON对象的数据jQuery的$。阿贾克斯() [英] Passing Multiple Json Objects as data using jQuery's $.ajax()

查看:170
本文介绍了传递使用多个JSON对象的数据jQuery的$。阿贾克斯()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴数据到一个MVC控制器和我试图保持状态和乐观并发。目前,我正在回发一个JSON请求,但将开放给可行的替代方案?

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.

我的问题是:有没有什么办法可以使用​​应用/ JSON作为内容类型回来后2 JSON对象

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?

在此先感谢!

更新:我解决我的问题,通过改变参数的contentType违约,而是发送字符串化的AJAX数据作为查询字符串单独命名参数。

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.

当您使用的contentType:应用/ JSON的;字符集= UTF-8,这推动数据到请求的主体,而不是查询字符串。我的新$。阿贾克斯()后,现在看起来是这样的:

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.

谢谢!

推荐答案

据我所知,目前还没有办法送不属于单亲JSON​​对象是儿童和有jQuery的德背面有两个完全不同的JSON对象使用阿贾克斯() C法是$ C $为您服务。你可以,我认为,有两个JSON对象回复字符串,然后使用一个外部的JSON库琴弦评估成JavaScript对象。

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.

这是否回答你的问题?

哎呀: 你问发布两个不同的JSON对象到控制器从jQuery的......吧?我的坏...是的,你可以这样做...只是改变这一行:

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),

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

遗憾的混淆。

Sorry about the mix-up.

这篇关于传递使用多个JSON对象的数据jQuery的$。阿贾克斯()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆