开发者

Capturing Session variables inside Jquery code

I use asp.net (webforms flavour) for the server coding. Let's say that I stored a variable called "test" inside the Session object.

Is there a way to grab its content inside the javascript js file (I use Jquery). Currently, I use the following workaround - in my aspx.cs file I populate a hidden field, then in Jquery I grab the contents of that field.

What is the proper way to do that?

I tried to expose the session through a web method and using $ajax function to get the value on the Jquery side, but I am getting security error - "permission denied" - no additional ex开发者_StackOverflow中文版planation offered.

My workaround works fine, but to me it seems rather clunky. What are the common ways to achieve session access in Jquery?


You could use jQuery to call a web method. Make sure you adorn your method with the [WebMethod(true)] attribute overload to make Session state available.


Since the session resides on the server, JQuery (on the client) really can't have "direct" access to it. If you want the page to be aware of a session variable's value, the best thing to do is usually to include the value on the dynamically-generated portions of the page, as you are doing with the hidden field. You can also produce javascript in your .aspx file, like this:

<script type="text/javascript">
    currentSessionName = '<%=Session["CurrentPersonName"].Replace("'", "\\'")%>';
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜