简体   繁体   中英

Get asp.net session variables from master file to external javascript library

So my asp.net website has session variables stored like this

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

I have an external javascript library and I need to send the variable to my library. So I want to this in my master file for aspx. My library reads from session storage from the browser but it seems like I cannot send session data from the server side to the client side. My next solution would be for my js library to directly access the variables through the master file.

<

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>

So how can I send candy to my library?

Using Ajax :

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

Code behind :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM