简体   繁体   English

如何从单独的JavaScript文件中获取会话变量?

[英]How can I get session variable from separate JavaScript file?

I have simple code in GoogleOauth2.js file 我在GoogleOauth2.js文件中有简单的代码

function storedAccsessToken(token) {
    $.ajax({
        type: "GET",
        url: '<%=HttpContext.Current.Request.ApplicationPath %>/SaveToken.ashx?accToken=' + token,
        dataType: "text",
        complete: function (resp, status) {
            if (status == "success" || status == "notmodified") {
                if (resp.responseText != '');
                alert(resp.responseText);
                window.location = window.location;

            }
        }

    });

}

and

function refresh() {
    var akkToken = '<%=Session["googleAccToken"]%>';
    $.ajax({
        url: 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=' + akkToken,
        data: null,
        success: function (resp) {
            user = resp;
            alert(user.name);
        },
        dataType: "jsonp"
    });

}

when i put js file in teg head 当我把js文件放在teg头

<head runat="server">
    <title></title>
    <script type="text/javascript" src="Scripts/jquery-1.8.2.min.js"></script>
    <script src="Scripts/GoogleOauth2.js" type="text/javascript"></script>

</head>

and when i start my project and debug js i see that <%=Session["googleAccToken"]%> compilator understand this as text but when i put js code in head tag between simple <script type="text/javascript"></script> it work fine and i get my value in session i want to know can i keep this code in separate file and that it worked fine 当我开始我的项目并调试js时,我看到<%= Session [“googleAccToken”]%> compilator将其理解为文本,但当我将js代码放在head标签之间的简单<script type="text/javascript"></script>它运行正常,我在会话中得到我的价值我想知道我可以将此代码保存在单独的文件中并且它工作正常

Your error is that you have include the <%= %> inside the javascript .js files, and they are not compile to give the results you expect - they stay as they are. 您的错误是您在javascript .js文件中包含<%= %> ,并且它们不会编译以提供您期望的结果 - 它们保持不变。

There are two possible solutions to that. 有两种可能的解决方案。 Ether include the variables before the javascript file, ether full move it to the page so the <%= %> can be compile. 以太包含javascript文件之前的变量,ether完全将其移动到页面,因此<%= %>可以编译。 For example, you can do that : 例如,你可以这样做:

<head runat="server">
    <script type="text/javascript" src="Scripts/jquery-1.8.2.min.js"></script>
    <script>
       var akkToken = '<%=Session["googleAccToken"]%>';
       var UrlTo = '<%=HttpContext.Current.Request.ApplicationPath %>SaveToken.ashx?accToken=';
    </script>
    <script src="Scripts/GoogleOauth2.js" type="text/javascript"></script>
</head>

and remove the akkToken from the GoogleOauth2.js file, and place url:UrlTo + token on the other line that also have the <%= %> 并从GoogleOauth2.js文件中删除akkToken ,并将url:UrlTo + token放在另一行也有<%= %>

It's because in the latter case the javascript was processed by .NET. 这是因为在后一种情况下,javascript是由.NET处理的。 When the script is part of the .NET page the <%= %> tags are processed and replaced by whatever value is calculated. 当脚本是.NET页面的一部分时, <%= %>标记将被处理并替换为计算的任何值。

When the script is an external file, it's requested by the browser separately (and even cached by the browser on subsequent page visits) and so .NET will not be processing it, resulting in the string '<%=Session["googleAccToken"]%>' . 当脚本是外部文件时,浏览器会单独请求它(甚至在后续页面访问时由浏览器缓存),因此.NET不会处理它,导致字符串'<%=Session["googleAccToken"]%>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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