如何根据JSON.stringfy(响应)重定向多个位置


how redirect multiple locations depending on JSON.stringify(response)

我试图通过ajax请求创建登录页面,但现在遇到问题,当用户输入错误的密码时,我希望它保持在同一页面上。登录成功后,它会跳到下一页。

 $('#login').on('click',function(event){
     event.preventDefault();
     var username = document.getElementById("username").value;
     var password = document.getElementById("password").value;
     if(password == ''||username == '')
            {
                alert ('Please fill the fields!');
            }
            $.ajax({
        url: "api/learnapi.php",
        type: "get",
        dataType: "json",
       data: {type: "login",email:username ,pass:password},
        //type: should be same in server code, otherwise code will not run
        ContentType: "application/json",
        success: function (response) {
            alert(JSON.stringify(response));
            location.href ="app-student-dashboard.php";
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
     });
  });

但当输入正确或错误的密码时,它会跳到app-student-dashboard.php。我该如何阻止它这样做。以下是我验证用户的代码

    $loname = $_GET['email'];
    $lopass = $_GET['pass'];
    $query1="select * from signup where email='$loname'";
    $result1= mysqli_query($conn,$query1);
    $row = mysqli_fetch_array($result1, MYSQLI_ASSOC);
 //  printf ("%s 'n", $row["password"]);
    $pass1=$row["password"];

    if( $pass1 == $lopass) {
       $res["flag"] = true;
        $rest["message"] = "Login successful."; 
      }
    if($pass1 != $lopass)
    {
     $res["flag"] = false;
        $rest["message"] = "Wrong password entered."; 
    }

这是否可能调用error:function(err)来发送"输入了错误的密码"的消息。

在得到@dadan的帮助后,我只是把成功的条件作为

success: function (response) {
              alert(JSON.stringify(response));
                    if(response.flag == true){
                             location.href ="app-student-dashboard.php";
                                  }
        },

得到了预期的结果。

我认为您应该将检查转移到成功内部,并设置条件,如果响应==true,则重定向页面。。