使用 javascript 分配会话值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17720916/
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
Assign session value using javascript
提问by Navajyoth CS
Is there any way to assign session value using javascript
有没有办法使用javascript分配会话值
i can retrive the value from session but assigning is not working
我可以从会话中检索值但分配不起作用
var TempSession = '<%= Convert.ToInt32(Session["Status"]) %>';
if(TempSession ==6)
{
alert(TempSession );
TempSession =1;
}
回答by Aditya Singh
Try using ajax call for setting the session value:-
尝试使用 ajax 调用来设置会话值:-
JS:-
JS:-
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script>
<script type="text/javascript">
var TempSession = '<%= Convert.ToInt32(Session["Status"]) %>';
if (TempSession == 6) {
alert(TempSession);
$.ajax({
type: 'POST',
url: 'WebForm1.aspx/SetValue',
data: '{ val:1 }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (msg) {
alert("New Session value is " + msg.d);
}
});
}
</script>
In your code behind file:-
在您的代码隐藏文件中:-
[WebMethod]
public static string SetValue(string val)
{
HttpContext.Current.Session["Status"] = val;
return HttpContext.Current.Session["Status"].ToString();
}
回答by Poni
If you're talking about server-side session then it really depends on the server-side.
如果您在谈论服务器端会话,那么它实际上取决于服务器端。
It should assign you a session id, usually through a cookie....
That means you could for example make an ajax request to a designated url in your server, specifying the required session id. That ajax request would then assign the session through a cookie.
它应该为您分配一个会话 id,通常通过 cookie ......
这意味着您可以例如向服务器中的指定 url 发出 ajax 请求,指定所需的会话 id。然后,该 ajax 请求将通过 cookie 分配会话。
Another idea would be to actively create a cookie (again, if server supports that) on the client-side with JavaScript with specific session id, i.e
另一个想法是在客户端使用具有特定会话 ID 的 JavaScript 主动创建 cookie(同样,如果服务器支持),即
$.cookie("SESSION", "session-id"); // Using jQuery
Although I've never tried that before.
Note above that the cookie's name depends on your server technology (Tomcat is JSESSIONID
by default[link], ASP.NET is ASP.NET_SessionId
[link], under "Note").
虽然我以前从未尝试过。
请注意,cookie 的名称取决于您的服务器技术(TomcatJSESSIONID
默认为[link],ASP.NET 为ASP.NET_SessionId
[link],在“注意”下)。
回答by Kees de Wit
You can transfer data to the server session by sending it through a XmlHttpRequest. The page or handler that recieves this data can put it in the server session data collection.
您可以通过 XmlHttpRequest 将数据发送到服务器会话。接收此数据的页面或处理程序可以将其放入服务器会话数据集合中。
回答by Alexander Manekovskiy
Actually there this a tutorial on MSDN that covers you case. It is described in Exposing Web Services to Client Script, see "Calling Static Methods in an ASP.NET Web Page" section.
实际上,MSDN 上有一个涵盖您案例的教程。它在将 Web 服务暴露给客户端脚本中进行了描述,请参阅“在 ASP.NET 网页中调用静态方法”部分。
All you have to do is to set EnablePageMethods
property to True
on your ScriptManager
control, then using PageMethods
object call static method (it should be decorated with WebMethod
attribute) declared in code behind.
你所要做的就是在你的控件上设置EnablePageMethods
属性,然后使用在后面的代码中声明的对象调用静态方法(它应该用属性装饰)。True
ScriptManager
PageMethods
WebMethod
回答by krishna
javascript function:
javascript函数:
function InitializeRequest(path) {
// call server side method
PageMethods.SetDownloadPath(path);
}
code behind function:
函数背后的代码:
[System.Web.Services.WebMethod]
public static string SetDownloadPath(string strpath)
{
Page objp = new Page();
objp.Session["strDwnPath"] = strpath;
return strpath;
}
Must enable page methods set to true:
必须启用设置为 true 的页面方法:
<asp:ScriptManager EnablePageMethods="true" ID="MainSM" runat="server" ScriptMode="Release" LoadScriptsBeforeUI="true"></asp:ScriptManager>