JavaScript不会在true语句上重定向

时间:2014-02-03 03:04:29

标签: javascript php ajax redirect

<label>Username:</label>
<input name="username" id="username" type="text" value="wawa">
<label>Password:</label>
<input name="password" id="password" type="password" value="123456">
<input value="Submit" name="submit" class="submit" type="submit" onclick='loginme();'>

function loginme () {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    var params = "username=" + username + "&password=" + password;
    var url = "login.php";
    $.ajax({
        type: 'POST',
        url: url,
        dataType: 'html',
        data: params,
        beforeSend: function () {                            
            document.getElementById("status").innerHTML = 'checking...';
        },
        complete: function () {
        }
        success: function (html) {  
            document.getElementById("status").innerHTML = html; // the code only executes until here

            if(red == "OK") { 
               window.location = 'account.php'; //it does not execute here i think.
               alert(html); // even here.
           }                                     
        }
    }

嗨,我知道,这个问题被多次询问过。但没有确切的答案。所以我在这里也有同样的问题。它不会将我重定向到帐户页面。与其他人一样,我也单独尝试重定向,它可以工作,但不是在实际的代码中。它已经用了四年的ajax编码,但这是我第一次遇到这个问题。我不知道代码发生了什么。即使我从头开始一切,它也行不通。感谢。

2 个答案:

答案 0 :(得分:0)

<html>
<head>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    function loginme() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    var params = "username=" + username + "&password=" + password;
    var url = "login.php";
    $.ajax({
        type: 'POST',
        url: url,
        dataType: 'html',
        data: params,
        beforeSend: function () {                            
            // document.getElementById("status").innerHTML = 'checking...';
        },
        complete: function () {
        },
        success: function (response) {              
            if(response == "OK") { 
               window.location.href = 'account.php'; //it does not execute here i think.
               alert(html); // even here.
           }                                     
        }
    });
    }
    </script>
</head>

<body>
<label>Username:</label>
<input name="username" id="username" type="text" value="wawa">
<label>Password:</label>
<input name="password" id="password" type="password" value="123456">
<input value="Submit" name="submit" class="submit" type="submit" onclick='loginme();'>


</body>
</html>

在login.php内部,如果一切顺利,请写echo "OK";exit;,然后您的页面将重定向到account.php

希望这会有所帮助!

答案 1 :(得分:0)

<html>
<head>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">
    function loginme() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;

    var params = "username=" + username + "&password=" + password;
    var url = "login1.php";
    $.ajax({
        type: 'POST',
        url: url,
        dataType: 'html',
        data: params,
        beforeSend: function () {                            
             document.getElementById("status").innerHTML = 'checking...';
        },
        complete: function () {
        },
        success: function (response) {        
              document.getElementById("status").innerHTML = response;
            if(response == "OK") { 
               window.location.href = 'account.php'; //it does not execute here i think.
               //alert(html); // even here.
           }                                     
        }
    });
    }
    </script>
</head>

<body>
<label>Username:</label>
<input name="username" id="username" type="text" value="wawa">
<label>Password:</label>
<input name="password" id="password" type="password" value="123456">
<input value="Submit" name="submit" class="submit" type="submit" onclick='loginme();'>
<div id='status'></div>

</body>
</html>

仍未使用<form>标记。但是我的旧代码运行时没有使用echo="OK"响应,而是使用布尔1 or 0,因此我使用intiger 1而不是字符串'OK'。