发送JavaScript的变量到服务器端ASP .NET [英] Send javascript variables to server-side ASP .NET

查看:83
本文介绍了发送JavaScript的变量到服务器端ASP .NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个JavaScript数据传递到后回服务器端。

I need to pass a javascript data to the server-side on post-back.

防爆结果
    VAR jsVariableToPass =新的对象();结果
    jsVariableToPass ['key1的'] =值;结果
    jsVariableToPass ['密钥2'] =值;结果
    jsVariableToPass ['KEY3'] =值3;

Ex
var jsVariableToPass = new Object();
jsVariableToPass ['key1'] = value1;
jsVariableToPass ['key2'] = value2;
jsVariableToPass ['key3'] = value3;

我想这是因为在服务器端一个Hashtable访问。什么是这样做的最佳方式?

I'd like this to be accessible as a Hashtable on the server side. What would be the best way of doing this?

我见过的唯一的帖子做到这一点使用一个隐藏的输入。这似乎有点哈克给我,我想知道是否有这样做的另一个更可以接受的方式。此外,如果这是唯一的方法,这将是反序列化和序列化这一点的最好方法是什么?

The only posts I've seen do this use a hidden input. This seems a little hacky to me and I was wondering if there was another more acceptable way of doing this. Also, if this is the only way, what would be the best way to deserialize and serialize this?

推荐答案

我想自己的评论稍后回答了你的问题。

I guess your own comment has answered your question.

您只需要在回发要传递的值。

You only want the values to be passed on postback.

只有这样,浏览器与服务器进行通信通过GET请求或POST请求通过HTTP协议,或者是

The only way browser and server can communicate is either via get request or post request over http protocol.

就像你说的,你只需要在后传递值返回,那么你就可以节省隐藏的输入和后散列表或使用YUI作为@Zoidberg在他的回答提到您可以模拟后回来。

As you said you only want values to be passed on post back , then you can save the hashtable in hidden input and post or you can simulate post back using YUI as @Zoidberg has mentioned in his answer.

我没有看到或者使用隐藏输入或使用YUI或其他JS库模拟回后作为少哈克以上哈克。

I do not see either using hidden input or using YUI or other JS library to simulate post back as less hacky or more hacky.

在这两种方法的主要思想是进行通信的服务器和那些是两种不同的方法来创建可通过浏览器发送到服务器的请求。

In both approach main idea is to communicate to server and those are two different ways to create a request which can be sent via browser to server.

和回答您的其他问题的最佳方式,序列化和反序列化

and to answer your other question about best way to serialize and deserialize

我会建议转换你的JavaScript数据JSON字符串按本例如并传递到服务器,并使用 litjson 以JSON数据转换为.NET对象。

I would recommend converting your javascript data to json string as per this example and pass that to server and using litjson to convert json data to .net objects.

这篇关于发送JavaScript的变量到服务器端ASP .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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