使用 jQuery 的 $.ajax() 将多个 Json 对象作为数据传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1545316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Multiple Json Objects as data using jQuery's $.ajax()
提问by dano
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?
我正在将数据发布到 MVC 控制器,并且我正在尝试为乐观并发维护状态。我目前正在发回一个 JSON 请求,但对可行的替代方案持开放态度吗?
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.
我还有一个(加密的)id 和时间戳数组,我想传回它,以便服务器可以解密它,然后在保存数据之前验证数据是否仍然新鲜。
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 notas a form submit.
数据对象是独立的并且不是一个或另一个的子对象或在包装器数组中(因为服务器端的反射反序列化)非常重要。同样重要的是要注意,我想异步执行此操作,而不是作为表单提交。
My question is: Is there any way I can post back 2 JSON objects using 'application/json' as the content type?
我的问题是:有什么办法可以使用“application/json”作为内容类型回发 2 个 JSON 对象?
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.
更新:我解决了我的问题,通过将 contentType 参数更改为默认值,而是将字符串化的 ajax 数据作为单独的命名参数发送到查询字符串中。
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:
当您使用 contentType: 'application/json; charset=utf-8',这会将数据推送到请求正文中,而不是查询字符串中。我的新 $.ajax() 帖子现在看起来像这样:
$.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
担
回答by KyleFarris
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 对象,它们不是单个父 JSON 对象的子对象,并让 jQuery 使用该.ajax()
方法为您解码。我想,您可以使用两个 JSON 对象作为字符串进行回复,然后使用外部 JSON 库将字符串评估为 Javascript 对象。
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:
Ooops:您是在询问从 jquery 向您的控制器发布两个不同的 JSON 对象,对吗?我的不好......是的,你可以这样做......只需更改这一行:
data: $.toJSON(data),
to:
到:
data: { json_1:$.toJSON(data_1), json_2:$.toJSON(data_2) },
Sorry about the mix-up.
抱歉搞混了。
回答by ShZ
If I understand you correctly, functional speaking, 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, while in latter it's explicit. It's still there either way, but if you want to send multiple objects in JSON you need to use the explicit approach.
唯一的区别是前一种情况下的数组是隐式的,而后一种情况下是显式的。无论哪种方式它仍然存在,但是如果您想以 JSON 发送多个对象,则需要使用显式方法。
So wrapping the two data objects in an array is really the ideal choice, but if your sever code isn't going to support that you'll need an alternative. The only possible method I can see to do this would be to put both data objects inside another object, like this:
因此,将两个数据对象包装在一个数组中确实是理想的选择,但如果您的服务器代码不支持,您将需要一个替代方案。我能看到的唯一可能的方法是将两个数据对象放在另一个对象中,如下所示:
var data = {};
data[0] = data1;
data[1] = data2;
And then you send data
along through the AJAX call.
然后你data
通过 AJAX 调用发送。
I would go so far as to say that the problem isn't your approach, but rather the JSON handling on the receiving end.
我什至要说问题不在于您的方法,而在于接收端的 JSON 处理。
回答by anish
You can do this in a simple way in asp.net ,if u just want to send multiple data to webmethod
如果您只想将多个数据发送到 webmethod,您可以在 asp.net 中以简单的方式执行此操作
data: '{userName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '",userName1: "' + $("#<%=TextBox1.ClientID%>")[0].value + '" }',
回答by Robert Koritnik
This is an answer after a really long time, but may be helpful to you for any future reference.
这是一个很长一段时间后的答案,但可能对您以后的任何参考有所帮助。
I suggest you read this stackoverflow answerthat is directly addressing your problem.
我建议您阅读直接解决您的问题的这个 stackoverflow 答案。
回答by CurlyFro
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);