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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Do you have an onError function hooked up to the call? That may shed some more light onto your issue: stackoverflow.com/questions/223308/…
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.
Here's an idea: Instead of using page methods, use a script service (msdn.microsoft.com/en-us/library/…) instead. Other people have had weird issues with page methods (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.
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>

4 Comments

You'll probably have to declare currentSessionName as a variable somewhere, like in your static .js file.
it's not that. This - '<%=Session["CurrentPersonName"].Replace("'", "\\'")%>' is the problem. Complains that there is a syntax error.
@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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.