tags:

views:

129

answers:

6

Is there any way to read Session value through JQuery?

Edited:

I am calling a .php file using JQuery. The .php file stores some column values in a session.

What would be the right approach to return those column values to the calling JQuery function?

A: 

Session values are stored on the server, JQuery is a client-side library that runs in the browser. Unless you send the session value down to the client, JQuery won't be able to read it.

Nebakanezer
+1  A: 

Sure you can set up a service which serves the Session values in JSON, and then use $.getJSON. But to read it directly is impossible.

Yuriy Faktorovich
A: 

I assume that you're referring to a server-side session in ASP.Net or PHP.

Not directly.

However, you could make an AJAX call to server-side code that returns something from the session.

If you do, beware of information disclosure.

SLaks
A: 

Session values can only be read on server-side. Still if you are really hard-bent on what you want to do, you can write a Ajax enabled web-method in your code-behind that responds with session value for the given key as argument. You can call this webmethod from JQuery and retrieve the session value!

this. __curious_geek
A: 

The ways sessions are associated with a client is by using cookies. That's where maybe the confusion takes place.

But the session data themselves are stored on the server.

Your backend need to send your session values somehow to your page for jQuery to pick it up.

Mike Gleason jr Couturier
+1  A: 

In PHP, something like (from memory as I've not PHP'd for some time...)

<input type="hidden" id="SessionValue" value="<?php echo $MySessionValue ?>">

Then in jQuery

$("#SessionValue").val();
James Wiseman