通过隐藏字段传递JSON序列数据 [英] Passing JSON serialized data via hidden field
问题描述
在ASP.NET的WebForms我想要的任意数据传递从服务器到客户端,然后再返回。我序列化到JSON和已被简单地制造的JavaScript创建该客户机上的对象。我没有问题,将数据发送到与阿贾克斯的服务器,但在有些情况下我也想送一个Javascript对象数据到服务器上回发。所以我想它需要在一个隐藏字段。
In ASP.NET WebForms I want to pass arbitrary data from server to client and back again. I am serializing to JSON and have been simply producing JavaScript that creates the object on the client. I have no problem sending data to the server with ajax, but there are situations where I also want to send a Javascript object data back to the server on postbacks. So I guess it needs to be in a hidden field.
这个一对夫妇的一般性问题。
A couple general questions about this.
1)什么是最小化的复杂性和优化的空间和效率方面做到这一点的最好方法是什么?在研究这个,我发现 Protocol Buffers的的但不似乎是一个很好的C#实现。我没有找到一个,但它是一对夫妇岁,自称为越野车,这样把我吓坏了。
1) What is the best way to do this in terms of minimizing complexity and optimizing space and efficiency? In researching this I discovered Protocol Buffers but there does not seem to be a good C# implementation. I did find one, but it was a couple years old and self-described as buggy so that scared me.
2)如果我只是通过JSON字符串,我怎么能肯定这将是安全的,包括作为一个隐藏字段的值?没有任何理由,我可能并不想这样做呢?我可以带的Base64 code,但看起来这增添了不少的开销。什么被认为是最好或preferred方法?
2) If I just pass a JSON string, how can I be sure it will be safe to include as the value for a hidden field? Is there any reason I might not want to do this? I could Base64 encode, but it seems like this adds a lot of overhead. What is considered the best or preferred method?
推荐答案
在过去,我通常做以下内容:
In the past, I've usually done the following:
- 创建一个C#对象,我可以轻松地序列化到XML(可净本身做到这一点,有JSON序列化在那里为好)。 (其实我觉得净可能会做JSON也...不知道虽然)
- 拿这个对象,通过Ajax请求通过一个隐藏字段或延迟加载它在客户端与客户分享。
- 然后客户端解析和操纵XML(jQuery的可以做到这一点),并发送回通过POST通过AJAX的服务器或隐藏字段。
- 最后,我从客户端反序列化的XML(或JSON)返回到一个对象,并从那里使用它。
所以,我不知道你想通过编码获得什么,但我觉得从JSON(或XML)转换对象以更小的东西为代价为任何包装/萎缩的好处太多的开销。 (除非你的超高流量的网站更关心带宽占用比客户端/服务器端处理)。
So I'm not sure what you're trying to gain through encoding, but I think the expense of transforming the object from JSON (or XML) to something smaller is too much overhead for any packaging/shrinking benefit. (Unless your a super high traffic site concerned more about bandwidth usage than server/client side processing.)
示例
但愿这给你如何做到这一点的想法。让我知道如果你有任何问题。
Hopefully this gives you an idea of how to accomplish this. Let me know if you have any more questions.
<html>
<head>
</head>
<body>
<input type="button" id="btnObject" value="Show Object" />
<input type="button" id="btnShowHid" value="Show Hidden Input Value" />
<input type="hidden" id="hidInput" value="" />
</body>
</html>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js'></script>
<script type='text/javascript' src='https://github.com/douglascrockford/JSON-js/raw/master/json2.js'></script>
<script>
//Your JSON Object
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
//jQuery Document Ready Event
$(function(){
//Get a reference to your hidden field
var $hidInput = $("#hidInput");
//Use json2.js library to convert the json object to a string
//and assign it to the hidden input's value
//NOTE: ASP.NET hidden input control reduces to a hidden input so you can treat them the same.
$hidInput.val(JSON.stringify(myJSONObject));
//Set up click events to view object and hidden value
$("#btnShowHid").click(function(){
alert($hidInput.val());
});
$("#btnObject").click(function(){
alert(myJSONObject);
});
});
</script>
这篇关于通过隐藏字段传递JSON序列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!