路过的JavaScript变量到服务器端的C#逻辑 [英] Passing javascript variables to server-side C# logic

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

问题描述

有没有指定/合格/ javascript变量复制到C#服务器端变量的方法吗?为了论证的缘故,让我们说,我是能够解析一些的JSON,我要存储和分配它们的客户端上的变量(即VAR =名字和VAR = 25和VAR = someDateTime等)。

Is there a way to assign/pass/copy a javascript variable to a server side variable in C#? For the sake of argument, let's say that I was able to parse some JSON for variables that I want to store and assign them on the client (ie. var = FirstName and var = 25 and var = someDateTime, etc) .

推荐答案

JavaScript变数在客户端上存在,所以为了得到这些值转换成你需要执行从客户端请求的服务器。你可能想叫AJAX的方法。 AJAX包含的JavaScript在你的页面的背景发出请求到服务器。你会建立一个C#网页,希望这些后台请求。如果使用GET请求,然后再放置在你的AJAX请求的查询字符串变量到新的C#页面。如果你想使用POST请求,那么你将设置在数据您发布到你的页面的参数。

Javascript variables exist on the client so in order to get those values into the server you'll need to execute a request from the client. You probably want an approach called AJAX. AJAX involves Javascript making requests to the server in the background of your page. You'll set up a C# web page that expects these background requests. If you use a GET request then then place the variables in the query string of your AJAX request to your new C# page. If you want to use a POST request then you'll set parameters in the data that you post to your page.

jQuery的库做出这种事情pretty简单。

Libraries like jQuery make this kind of thing pretty simple.

这篇关于路过的JavaScript变量到服务器端的C#逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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