0

用户需要提供特定密码才能被授权访问特定页面。

我显示一个表单,如果用户输入了正确的密码,将被重定向到新页面,否则需要显示一条消息,指示密码错误。我该如何实施?

目前,我可以使用以下代码显示“密码错误”消息,但如果密码正确,如何重定向用户。

如果我可以修改 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 进行重定向,但需要确保用户在重定向之前经过身份验证。

4

3 回答 3

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');
    })
于 2013-10-28T04:00:19.410 回答
1

我认为您可以使用 Java 从后端对用户进行身份验证。

验证后,密码错误或正确。根据这个结果,分别给出一个链接作为返回结果,并在会话或cookie中为当前用户设置一个状态。

当然,在渲染认证页面时,需要检查会话是否有效来决定是否显示内容。

您可以使用 php 或 asp 或任何动态网络语言来寻求帮助。

于 2013-10-28T04:33:49.500 回答
1

需要返回一个成功的页面作为其文本并使用以下内容

if(xmlhttp.responseText.trim() == 'Success')
            {
                 redirect code
            }
            else{
                     show error message
            }
于 2013-10-28T08:34:33.217 回答