最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net - How can I get session variable from separate JavaScript file? - Stack Overflow

programmeradmin0浏览0评论

I have simple code in GoogleOauth2.js file

function storedAccsessToken(token) {
    $.ajax({
        type: "GET",
        url: '<%=HttpContext.Current.Request.ApplicationPath %>/SaveToken.ashx?accToken=' + token,
        dataType: "text",
        plete: 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: '=' + akkToken,
        data: null,
        success: function (resp) {
            user = resp;
            alert(user.name);
        },
        dataType: "jsonp"
    });

}

when i put js file in teg head

<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"]%> pilator 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

I have simple code in GoogleOauth2.js file

function storedAccsessToken(token) {
    $.ajax({
        type: "GET",
        url: '<%=HttpContext.Current.Request.ApplicationPath %>/SaveToken.ashx?accToken=' + token,
        dataType: "text",
        plete: 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./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

<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"]%> pilator 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

Share Improve this question edited Oct 29, 2012 at 13:50 Tim M. 54.4k14 gold badges124 silver badges166 bronze badges asked Oct 29, 2012 at 13:44 HaikMnatsakanyanHaikMnatsakanyan 3032 gold badges5 silver badges13 bronze badges 2
  • You should be able to...though you likely shouldn't. What have you tried – Matt Whipple Commented Oct 29, 2012 at 13:47
  • if it's impossible i just want why this worked when i put code in head tag – HaikMnatsakanyan Commented Oct 29, 2012 at 13:55
Add a ment  | 

2 Answers 2

Reset to default 5

Your error is that you have include the <%= %> inside the javascript .js files, and they are not pile to give the results you expect - they stay as they are.

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 pile. 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 <%= %>

It's because in the latter case the javascript was processed by .NET. When the script is part of the .NET page the <%= %> tags are processed and replaced by whatever value is calculated.

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"]%>'.

发布评论

评论列表(0)

  1. 暂无评论