用户需要提供授权才能访问特定页面的特定密码。
我显示一个表单,如果用户输入正确的密码,将被重定向到新页面,否则需要显示一条消息,指出密码错误。我该如何实施呢?
目前,我可以使用以下代码显示“密码错误”消息,但如果密码正确,如何重定向用户。
如果我可以修改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;
}
Java
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进行重定向,但需要确保在重定向之前对用户进行身份验证。
答案 0 :(得分:1)
$.getJSON( url, myDataForTheServer)
.done(function (dataReturned) {
console.log(dataReturned);
})
.fail(function (response, status, statusText) {
console.log(status + statusText);
})
.always(function () {
console.log('the end');
})
答案 1 :(得分:1)
我认为您可以使用Java从后端验证用户身份。
验证后,密码错误或正确。根据此结果,分别提供一个链接作为返回结果,并在会话或cookie中设置当前用户的状态。
当然,在渲染经过身份验证的页面时,您需要检查会话是否有效以决定是否显示内容。
您可以使用php或asp或任何动态网络语言寻求帮助。
答案 2 :(得分:1)
需要返回一个成功的页面作为其文本并使用以下
if(xmlhttp.responseText.trim() == 'Success')
{
redirect code
}
else{
show error message
}