Problem with window.location

1

I'm having problems with window.location = "index.php" , it's not redirecting from page login.php to index.php , see code:

<script type="text/javascript">
  $(function() {
    $("#loginForm").on("submit", function(a) {
      a.preventDefault(), $("#signinButton").attr("value", "Autenticando...");

      var user = $("#username").val();
      var pass = $("#password").val();

      if (user == "") {
        $("#message").css("display", "block"), $('#message').html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Insira seu usuário do Twitter</div>"), $("#signinButton").attr("value", "Entrar");
        return false;
      }

      else if (pass == "") {
        $("#message").css("display", "block"), $('#message').html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Insira sua senha do Twitter</div>"), $("#signinButton").attr("value", "Entrar");
        return false;
      } else {
        $("#message").hide();
      }

      var b = $("#username").val();

      0 == /^[a-zA-Z0-9_ ]*$/.test(b) ? ($("#message").css("display", "block"), $("#message").html("<div class='alert alert-danger'><i class='fa fa-warning'></i> Existem caracteres especiais no seu usuário. Se estiver usando <strong>@</strong> remova-o!</div>"), $("#signinButton").attr("value", "Entrar")) : $.ajax({
        type: "POST",
        url: "api/login.php",
        dataType: "JSON",
        data: $("#loginForm").serialize(),
        success: function(a) {
          1 == a.redirect ? window.location = "index.php" : ($("#message").css("display", "block"), $("#message").html(a.message)), $("#signinButton").attr("value", "Entrar");
        }
      })
    })
  })
</script>

Here is my response code is much larger than this but I will only post the verification:

if (isset($cookies['auth_token'])) {
    Cookies::set('auth_token',            $cookies['auth_token']);
  Cookies::set('password',              $ttrPassword);

    $_SESSION[SITE_NAME . '_session'] = $ttrUsername;

    echo json_encode(array('status' => 'success','message'=> "<div class='alert alert-success'><i class='fa fa-check'></i> Autenticação bem sucedida, estamos te redirecionando</div>"));
} else {
    echo json_encode(array('status' => 'error','message'=> "<div class='alert alert-danger'><i class='fa fa-warning'></i> Não foi possível autenticar com o Twitter.</div>"));
}

What problem is it in json's return, or is my javascript incorrect?

    
asked by anonymous 30.06.2017 / 23:02

1 answer

3

In your PHP you forgot to set the redirect position you compare in JS.

1 == a.redirect ? window.location = "index.php" : ....

So just set a value for it in your PHP that will work:

 echo json_encode(
         array('status' => 'success',
                'message'=> "<div class='alert alert-success'><i class='fa fa-check'></i> Autenticação bem sucedida, estamos te redirecionando</div>",
                "redirect"=>$algum_valor));
    
30.06.2017 / 23:43