javascript 通过隐藏字段传递 JSON 序列化数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5173292/
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 JSON serialized data via hidden field
提问by Jamie Treworgy
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.
在 ASP.NET WebForms 中,我想将任意数据从服务器传递到客户端,然后再返回。我正在序列化为 JSON,并且一直在简单地生成在客户端上创建对象的 JavaScript。我使用 ajax 向服务器发送数据没有问题,但在某些情况下,我还想在回发时将 Javascript 对象数据发送回服务器。所以我想它需要在一个隐藏的领域。
A couple general questions about this.
关于这个的几个一般性问题。
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 Buffersbut 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.
1) 在最小化复杂性和优化空间和效率方面,最好的方法是什么?在研究这个时,我发现了Protocol Buffers,但似乎没有一个好的 C# 实现。我确实找到了一个,但它已经有几年的历史了,并且自称是越野车,这让我很害怕。
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?
2)如果我只是传递一个 JSON 字符串,我怎么能确定作为隐藏字段的值包含在内是安全的?我有什么理由不想这样做吗?我可以使用 Base64 编码,但这似乎增加了很多开销。什么被认为是最好的或首选的方法?
采纳答案by Brandon Boone
In the past, I've usually done the following:
过去,我通常会执行以下操作:
- Created a C# object that I can easily serialize to XML (.Net can do this natively and there are JSON serializers out there as well). (Actually I think .Net may do JSON as well... not sure though)
- Take this object and share it with the client via a hidden field or lazy load it on the client via an AJAX request.
- The client then parses and manipulates the XML (jQuery can do this) and sends it back to the server via AJAX or a hidden field via a POST.
- Lastly I deserialize the XML (or JSON) from the client back into an Object and work with it from there.
- 创建了一个 C# 对象,我可以轻松地将其序列化为 XML(.Net 可以在本机执行此操作,并且还有 JSON 序列化程序)。(实际上我认为 .Net 也可以做 JSON ......虽然不确定)
- 获取这个对象并通过隐藏字段与客户端共享它,或者通过 AJAX 请求将它延迟加载到客户端。
- 客户端然后解析和操作 XML(jQuery 可以做到这一点),并通过 AJAX 或通过 POST 的隐藏字段将其发送回服务器。
- 最后,我将来自客户端的 XML(或 JSON)反序列化回一个对象并从那里使用它。
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.)
因此,我不确定您想通过编码获得什么,但我认为将对象从 JSON(或 XML)转换为较小的对象的开销对于任何打包/收缩的好处来说都是太大的开销。(除非您的超高流量站点更关心带宽使用而不是服务器/客户端处理。)
EXAMPLE
例子
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>

