如何在登录后忽略重定向并重定向另一个页面

时间:2015-10-30 01:13:50

标签: javascript html ajax redirect

我想知道如何在执行登录后忽略服务器的转移,然后将您重定向到另一个页面,对它进行了大量研究,找不到真正有用的东西,已经尝试使用window.location, window.location .href,window.location.assign但没有效果。 我到目前为止最好的只是使用window.open打开一个新页面到我想要的网站,但我一直被重定向。

<HTML>
<HEAD>
<TITLE>ECORI LOGIN</TITLE>
<script language="JavaScript" type="text/javascript">
window.onbeforeunload = function(){
        window.open("mypage.html");
    }
</script>

<BODY>
    <FORM action="http://mylogin.com/security/jspring_security_check" method="post" >
    Usuario: <INPUT type="text" name="j_username"><BR>
    Senha: <INPUT type="password" name="j_password"><BR>
    <INPUT type="submit" value="Login" onclick="function">
    </FORM>
</BODY>
</HTML>

欢迎所有帮助,对不起我的英语。 非常感谢!

2 个答案:

答案 0 :(得分:1)

我看到使用AJAX建议的其中一条评论,这只是另一种方法。 xhr基本上是AJAX但没有铃声和口哨声。老实说你应该使用AJAX。它更清洁。

 function doPost(url, method, callback, data){
    xhr = new XMLHttpRequest();
    xhr.open(method, url, true);
    xhr.send(data);
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            if(typeof callback() != 'undefined')
                callback(xhr.responseText);
        }
    }
}
function handleLogin(respObj){
console.log(respObj);
//Do what needs to be done here after form submit...
}
$(function(){
    $("form input:button").on('click', function(e){
        formid = "loginForm";
        var form = document.getElementById(formid);
        var formdata = new FormData(form);
        doPost("http://mylogin.com/security/jspring_security_check", "post",     "handleLogin", formdata);
    });
});

我改变了你的表格:

<FORM method="post" id="loginForm">
    Usuario: <INPUT type="text" name="j_username"><BR>
    Senha: <INPUT type="password" name="j_password"><BR>
    <INPUT type="button" value="Login">
</FORM>

如果你想使用AJAX,你可以使用它代替doPost

formid = "loginForm";
var form = document.getElementById(formid);
var formdata = new FormData(form);
$.ajax({
url: 'http://mylogin.com/security/jspring_security_check',
data: formdata,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
handleLogin(data);
}
});

答案 1 :(得分:0)

提交表单的行为基本上是告诉浏览器离开页面,将表单数据发送到action所在的位置,最后在该页面中。你有两个选择:

  1. 使用JavaScript拦截您的登录信息,而是使用AJAX进行登录。一旦服务器响应您的AJAX请求,根据响应,您就可以使用JavaScript将用户重定向到您想要的任何位置。

  2. 您似乎使用Java。我相信您可以更改配置,以便在登录成功后将您重定向到所需的页面。这比黑客攻击JS解决方案更安全。就像

    form -(submit)-> jspring_security_check -(server redirect)-> final destination