【发布时间】:2020-07-23 05:40:02
【问题描述】:
我在页面加载时测试一个空的会话变量,如果为真,则请求一个调用包含文件的 AJAX 函数,一个隐藏的覆盖 HTML 登录表单,其中 ASP 脚本通过 JS 可见。这按预期工作。
提交表单后,我会测试空密码、错误密码和正确密码。这也很好用。
但是,一旦测试了这些条件,调用 JS 或设置 vbscript 条件变量来隐藏覆盖的表单都没有效果。
通过导航并按 F5 或在输入并提交表单后简单地执行此操作来清除缓存,以清除覆盖并证明已设置会话变量。
这里有两个问题:-
-
为什么每次调用 ASP 页面都不刷新?我通过切换可见文本行、保存表单、然后通过其提交按钮调用表单来对此进行测试。
-
为什么嵌入的 JS 或 ASP 脚本变量没有效果。我用 console.log 测试 JS。
首先是JS代码:
function checkLogin() {
var password = document.getElementById("password").value;
var userID = document.getElementById("UserID").value;
if (password != null) {
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
} else {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log("xxx: " + document.getElementById("session").value);
if (document.getElementById("session").value == true) {
document.getElementById("overlay").style.display = "none";
document.getElementById("login").style.display = "none";
}
document.getElementById("password").innerHTML = password;
}
};
xmlhttp.open("GET", "includes/checkpw.asp?u=" + userID + "&p=" + password);
xmlhttp.send();
}
}
还有包含文件:
<%
response.expires = -1
set conn = server.createobject("ADODB.connection")%>
<!--- #include virtual=/includes/connpath.asp -->
<%querySQL = "qryselUser"
set recset = conn.execute(querySQL)
if not recset.bof and not recset.eof then
arrUser = recset.getrows
countUser = ubound(arrUser,2)
end if%>
<%if Request.Form("formbtn") = "Cancel" then
Session.Abandon
conn.close()
set conn = nothing
Response.Redirect "default.asp"
end if
querySQL = "qryselUser"
set recset = conn.execute(querySQL)
if not recset.bof and not recset.eof then
arrUser = recset.getrows
countUser = ubound(arrUser,2)
end if%>
<%UserID = cint(request.querystring("u"))
password = request.querystring("p")
if password <> "" then
querySQL = "qryselLogin'" & UserID & "'"
set recset = conn.execute(querySQL)
arrUser = recset.getrows
recset.close
set recset = nothing
if UserID = arrUser(0,0) and password = arrUser(2,0) then
session("id") = arrUser(0,0)
session("level") = arrUser(3,0)
session("lock") = arrUser(4,0)
sessiontrue = true
conn.close()
set conn = nothing%>
<%else
userError = "Either Username or Password is incorrect"%>
<script>console.log("Error")</script>
<%querySQL = "qryselUser"
set recset = conn.execute(querySQL)
arrUser = recset.getrows
countUser = ubound(arrUser,2)
recset.close
set recset = nothing
end if
end if%>
<form method="post" action="adminsite.asp" class="formblock" action="adminsite.asp">
<h2>Login</h2>
<input id="todo" name="todo" type="hidden" value="login" />
<input id="session" name="session" type="hidden" value="<%=sessiontrue%>" />
<label for="UserID">Username</label>
<select name="UserID" id="UserID" onkeypress="return nextfield('password')">
<%for m = 0 to countUser%>
<option value="<%=arrUser(0,m)%>"<%if UserID = arrUser(0,m) then%> selected='selected'<%end if%>><%=arrUser(1,m)%></option>
<%next%>
</select><br />
<label for="password">Password</label><input id="password" name="password" value="<%=password%>" onkeypress="return nextfield('formbtn')" type="password" maxlength="50" size="20" autocomplete="on" />
<a class="showpw" id="showpw" onclick="showLoginPw();">Show Password</a><br />
<%if userError <> "" then%>
<span class="warn"><%=userError%></span>
<span class="forgot">Forgot password? <a href="forgotpassword.asp" target="_blank">Request a new one</a>.</span>
<%end if%>
<input type="button" onclick="checkLogin();checkSession();" value="Login" class="formbtn" name="formbtn" id="formbtn" />
<!-- <a class="formbtn" onclick="" target="checkLogin">Login</a>-->
<input type="submit" value="Cancel" class="formbtn" name="formbtn" name="formcancel" id="formcancel" />
<%if session("id") <> "" then%>
<script>
document.getElementById('overlay').style.display='none';
document.getElementById('login').style.display='none';
</script>
<%end if%>
</form>
<%if session("id")<>"" then
response.Write "<p>xxx: "& userError &"</p>"
end if%>
我被难住了。而且我显然不知道自己在做什么,因此请求帮助。
【问题讨论】:
-
显示的asp代码,是ajax调用中调用的页面吗?还是显示的 JavaScript 和 asp 在同一页面中?您提到“包含文件”有点令人困惑。
-
您意识到将
password作为查询字符串传递,它将显示在浏览器中并且还会记录在网站日志文件中?这种做法不受欢迎,可能被视为安全风险。 -
是的,ajax调用调用asp文件,该文件本身就是主页正文中的一个包含文件。正如您正确指出的那样,使用查询字符串是愚蠢的。我已将 xmlhttp 更改为 post 请求,并在 asp 文件中适当地更新了代码。
标签: javascript ajax asp-classic