用户需要提供特定密码才能被授权访问特定页面。
我显示一个表单,如果用户输入了正确的密码,将被重定向到新页面,否则需要显示一条消息,指示密码错误。我该如何实施?
目前,我可以使用以下代码显示“密码错误”消息,但如果密码正确,如何重定向用户。
如果我可以修改 xmlhttp 对象的返回码,我可能能够满足要求。
function auth(){
var form = $('#form').serialize();
if(window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("background").style.display = "Block";
document.getElementById("box").style.display = "Block";
document.getElementById("results").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("get","../authenticate?"+form,false);
xmlhttp.send();
return false;
}
爪哇
public String authenticate()
{
if(user is authenticated)
return a success parameter to JavaScript
else
return a wrong password message to JavaScript
}
附加信息
后端是Java。
我知道我可以使用 window.location.href 进行重定向,但需要确保用户在重定向之前经过身份验证。