开发者

How to read session values using jQuery

开发者 https://www.devze.com 2023-02-04 23:58 出处:网络
I am using c# and jQuery. I have below code where I am setting the Session Variable using C# code. if (!string.IsNullOrEmpty(results))

I am using c# and jQuery.

I have below code where I am setting the Session Variable using C# code.

if (!string.IsNullOrEmpty(results))
{
    string[] array = results.Split(',');
    string firstName = array[0];
    string lastName = array[1];
    string activeCardNo = array[2];
    string memberShipTier = array[3];
    string accessToken = array[4];

    Session["skyFirstName"] = firstName.ToString();
    Session["skyLastName"] = lastName.ToString();
    Session["skyActiveCardNo"] = activeCardNo.ToString();
    Se开发者_JAVA技巧ssion["skyMemberShipTier"] = memberShipTier.ToString();
    Session["boolSignOn"] = "true";
    Response.Redirect(fromPage);
    Response.End();
}

Now I want to read these values (Session["skyFirstName"]) using jQuery so that I can set in my elements. Please suggest.


Session values are stored on the server and it is impossible to read them with client side javascript. One way to achieve this would be to expose some server side script or generic handler which would return the corresponding session value given a key and then use jQuery to send an AJAX request to this handler and read the value. You should be aware that by doing this the user can read all his session values. Be warned that exposing the same script for writing session values could be catastrophic from security standpoint.

Here's an example:

public class ReadSession : IHttpHandler, IReadOnlySessionState
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Write(new JavaScriptSerializer().Serialize(new
        {
            Key = context.Request["key"],
            Value = context.Session[context.Request["key"]]
        }));
    }

    public bool IsReusable 
    { 
        get { return true; } 
    }
}

and then query it:

$.getJSON('/ReadSession.ashx', { key: 'skyFirstName' }, function(result) {
    alert(result.Value);
});


jquery runs on the client, which cannot directly access your server-side-session values. one solution is to provide a webservice which returns these values and use the webservice, another one would be to include the values in the page-response as JSON (e.g.) and access them on the client.


You cannot access the session variables with javascript as the session variables are server side rather than client side.

One work around that has already been mentioned is to use ajax to allow the javascript to communicate with the server side. This is fine, but possibly overly complicated for what you need.

Another, simpler solution would be to output the session variables into hidden input fields or as javascript variables in script tags which you can then access with the javascript.


jQuery is javascript, so in order to have those variables available you need to print out html code (at least one script tag) where you set the read-out session variables from C# as javascript variable.

An alternative would be to use an ajax request to get the session variables from a server script.

0

精彩评论

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