如何通过 javascript 从 asp 服务器脚本获取会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15926513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a session variable via javascript from an asp server script
提问by user2225394
I'm new to the javascript world and have a simple test to read session vars in javascript:
我是 javascript 世界的新手,有一个简单的测试来读取 javascript 中的会话变量:
my asp file:
我的asp文件:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Session("id")=1234
Session("code")="ZZ"
%>
my html file:
我的 html 文件:
<html>
<head></head>
<body>
<script type="text/javascript" src="asp/testSession.asp">
alert("Session ID " + Session("id"));
</script>
</body>
What am I doing wrong?
我究竟做错了什么?
回答by mellamokb
All ASP code has to be placed between <%
and %>
tags to be processed server-side:
所有ASP代码必须放置之间<%
和%>
服务器端代码被处理:
alert("Session ID " + <%=Session("id") %>);
^^^ add tags ^^
Also, you can use <%=
as a shortcut to output a variable. It's short for Response.Write
.
此外,您可以将其<%=
用作输出变量的快捷方式。它是 的缩写Response.Write
。
回答by VirtualTroll
You cannot mix javascript and asp in the way you did it. Javascript is executed locally while asp is compiled by the server and then send to your browser.
您不能按照您的方式混合使用 javascript 和 asp。Javascript 在本地执行,而 asp 由服务器编译,然后发送到您的浏览器。
When the page reaches your browser, only the product of the asp compilation remains. In order to use the value or print it, you should do the following :
当页面到达您的浏览器时,只剩下 asp 编译的产品。为了使用该值或打印它,您应该执行以下操作:
<html>
<head></head>
<body>
<script type="text/javascript" src="asp/testSession.asp">
alert("Session ID " + <%=Session("id")%>);
</script>
</body>