0

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 explanation offered.

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

Orbling
  • 20,413
  • 3
  • 53
  • 64
sarsnake
  • 26,667
  • 58
  • 180
  • 286

2 Answers2

1

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.

Community
  • 1
  • 1
Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
  • no, nothing works. so frustrating. I set up the EnablePageMethods="true" and the value returned by the WebMethod is still undefined. My other choice is to simply spit it out to the hidden field and pick it up from there, which seems rather barbaric. – sarsnake Dec 17 '10 at 22:46
  • Do you have an onError function hooked up to the call? That may shed some more light onto your issue: http://stackoverflow.com/questions/223308/asp-net-page-methods-returning-undefined – Aaron Daniels Dec 20 '10 at 17:05
  • I do. I am now completing other parts of the project before I get back to this one. I do want to get to the bottom of this, thank you for your answer - this way does seem the most appropriate way to pass the server variables to Jquery code. Currently, I am just writing it out to the hidden field, but this will have to change. – sarsnake Dec 20 '10 at 19:37
  • Here's an idea: Instead of using page methods, use a script service (http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx) instead. Other people have had weird issues with page methods (http://www.west-wind.com/weblog/posts/152493.aspx), especially when master pages are involved. Isolating the functionality in its own service is *normally* better decoupling anyways. Using the WebMethod(true) overload, thereby allowing access to the user's session, still applies. – Aaron Daniels Dec 21 '10 at 18:37
0

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>
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • You'll probably have to declare currentSessionName as a variable somewhere, like in your static .js file. – StriplingWarrior Dec 01 '10 at 16:08
  • it's not that. This - '<%=Session["CurrentPersonName"].Replace("'", "\\'")%>' is the problem. Complains that there is a syntax error. – sarsnake Dec 01 '10 at 20:33
  • @gnomixa: that should be getting evaluated on the server side, so it should never look like that in the javascript. You're sure you put it in an aspx file? – StriplingWarrior Dec 01 '10 at 20:38
  • nope still get an error: "Compiler Error Message: BC30518: Overload resolution failed because no accessible 'Write' can be called with these arguments". I put the block inside my header on aspx page. I guess I am just going to do it the barbaric way, as it's taking way too long to make it work the proper way. If there is a proper way. – sarsnake Dec 17 '10 at 23:01