【问题标题】:Get asp.net session variables from master file to external javascript library将 asp.net 会话变量从主文件获取到外部 javascript 库
【发布时间】:2018-04-15 08:40:59
【问题描述】:

所以我的 asp.net 网站有这样存储的会话变量

Request.ServerVariables['candy'] 
session['candy'].

我有一个外部 javascript 库,我需要将变量发送到我的库。所以我想在我的 aspx 主文件中使用它。我的库从浏览器的会话存储中读取,但似乎我无法将会话数据从服务器端发送到客户端。我的下一个解决方案是让我的 js 库通过主文件直接访问变量。

script type="text/javascript" src="<%: Url.Content("~file.js")%>"></script>  

     <% 

            if (Session["candy"] != null)
                candy= (string)Session["candy"]; 
     %>


    <script>
    var candy = '<%: Request.ServerVariables["candy"]%>'';
    </script>

那么我怎样才能把糖果送到我的图书馆呢?

【问题讨论】:

    标签: javascript c# html asp.net session


    【解决方案1】:

    使用 Ajax:

    $.ajax({
        url: 'YourController/GetCandy',
        type: "get",
        success: function (result) {
            candy = result;
        }
    });
    

    后面的代码:

    public string GetCandy() {
        return ((Session["candy"] != null) ? Session["candy"] : "");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 2011-11-25
      • 2014-03-29
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      相关资源
      最近更新 更多