简体   繁体   English

jQuery ajax调用后如何重定向页面?

[英]How to redirect page after the jquery ajax is call?

This is my Login form code. 这是我的登录表单代码。 My problem is after the statement is true. 我的问题是陈述正确之后。 Is will not be redirect to the home page instead it's just show the home page at my index page. Is不会重定向到主页,而是仅在我的索引页上显示主页。

How to Redirect jquery ajax at the other page after the statement is true? 语句为true后,如何在另一页重定向jquery ajax?

HTML Code HTML代码

<form id="myForm" class="form-group" method="post" action="actions/login_check.php">    
    <div id="result"></div> 
    <table class="table-condensed" >
        <tr><br>
          <td><input type="text" name="username" class="form-control" autofocus="autofocus" placeholder="Username" size="40"></td>
        </tr>
        <tr>
            <td><input type="password" name="password" placeholder="Password" class="form-control"></td>
        </tr>
        <tr>
            <td><button id="submit" class="btn btn-primary form-control">Sign In</button></td>
        </tr>
        <tr>
            <td align="right" style="font-size:14px;"><a href="forgot_password.php">Forgot Password?</a></td>
        </tr>
    </table>
</form>

PHP code PHP代码

$username = mysqli_real_escape_string($con,$_POST['username']);
$password = mysqli_real_escape_string($con,sha1($_POST['password']));
$empty = "da39a3ee5e6b4b0d3255bfef95601890afd80709";

$query = mysqli_query($con,"SELECT * FROM users WHERE username = '".$username."' AND password = '".$password."'");
$row = mysqli_num_rows($query);
$getUser = mysqli_fetch_array($query);


if(empty($username) or $password == $empty)
    echo "Invalid Email or Password";
elseif($username == $getUser['username'] and $password ==  $getUser['password']){
    session_start();
    $_SESSION['username'] = $username;
    $online = "UPDATE users SET active = 1 WHERE username = '".$username."'";
    mysqli_query($con,$online);
    header("Location: ../home.php");
    $_SESSION['timestamp'] = time();
}else
    echo "Invalid Email or Password";

jQuery Ajax jQuery Ajax

    $("button#submit").click(function(){

    if( $("#username").val == "" || $("#password").val == "")
        $("div#result").html("Please enter username and password");
    else
        $.post( $("#myForm").attr("action"),
            $("#myForm :input").serializeArray(),
            function(data) {
                $("div#result").html(data);
            }
         );

         $("#myForm").submit(function(){
            return false;
         });
});

In the POST response, you can use window.location.href 在POST响应中,可以使用window.location.href

function(data) {
    $("div#result").html(data);
    window.location.href=<homepageurl> 
}

As of jQuery 1.8, you can use .done to redirect the user after a successful jquery .post . 从jQuery 1.8开始,可以在成功使用jquery .post之后使用.done重定向用户。

  .done(function() {
    window.location.href='http://www.successpage.com'; 
  })

ie: 即:

$("button#submit").click(function(){

if( $("#username").val == "" || $("#password").val == "")
    $("div#result").html("Please enter username and password");
else
    $.post( $("#myForm").attr("action"),
        $("#myForm :input").serializeArray(),
        function(data) {
            $("div#result").html(data);
        }).done(function() {
    window.location.href='http://www.successpage.com'; 
  });

     $("#myForm").submit(function(){
        return false;
     });

});

There are plently of options. 有很多选择。
1) You can assign window.location.href the URL which you want to redirect. 1)您可以分配window.location.href您要重定向的URL。 But there is a problem. 但有一个问题。

  • Using ajax, data loads asynchronously, so there might be cases when the page redirects in between the ajax response. 使用ajax,数据异步加载,因此在某些情况下,页面可能会在ajax响应之间进行重定向。
  • To overcome this problem, use .done() method to redirect when the ajax is finished . 要解决此问题,请在ajax完成时使用.done()方法进行重定向 Inside that done() method, pass an anonymous function, and use window.location.href inside it. 在该done()方法内,传递一个匿名函数,并在其中使用window.location.href。
    2) You can also use .load() method which is also the short hand for .ajax() to perform page load without the page refresh which gives user a sense of redirection. 2)您也可以使用.load()方法,这也是.ajax()执行页面加载的捷径,而无需刷新页面,这给了用户重定向的感觉。 So its your choice. 因此,这是您的选择。 Pick any one! 选择任何一个!

    Just try window.location 只需尝试window.location

    success: function (response) {
                   if (response.d == true) {
                        window.location = "directed_page";
                    }
                },
    

    or try to use 或尝试使用

    window.location.replace()
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM