如何使用 jQuery 读取会话值

发布于 2024-10-12 03:27:31 字数 739 浏览 7 评论 0原文

我正在使用 C# 和 jQuery。

我有下面的代码,我使用 C# 代码设置会话变量。

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();
    Session["skyMemberShipTier"] = memberShipTier.ToString();
    Session["boolSignOn"] = "true";
    Response.Redirect(fromPage);
    Response.End();
}

现在我想使用 jQuery 读取这些值 (Session["skyFirstName"]),以便我可以在我的元素中进行设置。请建议。

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();
    Session["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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

因为看清所以看轻 2024-10-19 03:27:31

会话值存储在服务器上,无法使用客户端 JavaScript 读取它们。实现此目的的一种方法是公开一些服务器端脚本或通用处理程序,该脚本或通用处理程序将返回给定键的相应会话值,然后使用 jQuery 向该处理程序发送 AJAX 请求并读取该值。您应该知道,通过这样做,用户可以读取他的所有会话值。请注意,从安全角度来看,公开用于写入会话值的相同脚本可能是灾难性的。

这是一个例子:

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; } 
    }
}

然后查询它:

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

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);
});
久随 2024-10-19 03:27:31

jquery 在客户端上运行,无法直接访问服务器端会话值。一种解决方案是提供一个返回这些值的 Web 服务并使用该 Web 服务,另一种解决方案是将这些值作为 JSON(例如)包含在页面响应中并在客户端上访问它们。

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.

忆伤 2024-10-19 03:27:31

您无法使用 javascript 访问会话变量,因为会话变量是服务器端而不是客户端。

已经提到的一种解决方法是使用 ajax 来允许 javascript 与服务器端进行通信。这很好,但对于您的需要来说可能过于复杂。

另一个更简单的解决方案是将会话变量输出到隐藏的输入字段中或作为脚本标记中的 javascript 变量,然后您可以使用 javascript 访问这些变量。

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.

飘逸的'云 2024-10-19 03:27:31

jQuery 是 javascript,因此为了使这些变量可用,您需要打印 html 代码(至少一个脚本标记),在其中将 C# 中的读出会话变量设置为 javascript 变量。

另一种方法是使用 ajax 请求从服务器脚本获取会话变量。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文