Ajax Success - actions at Ajax response time

1

Let's say: During AJAX communication with my PHP I need to test a condition and if the condition is satisfied I want the page to go back to index.html

    $('#tabela').empty();
    $retorno=0;
    $.ajax({
        data: {funcao: 'listaMeusEstabs', idcli: getUrlVars()["idcli"], idsess: getUrlVars()["idsess"]},
        type:'post',    
        dataType: 'json',
        url: $servidor,
        success: function(dados){
            $retorno=1;
            $('#tabela').append("<tr><td> ---TUDO OK---</td></tr>");                    
        },
        complete: function(dados){
            if ($retorno==0){
                $linha = "<tr><td>Sem registros</td></tr>";
                $('#tabela').append("<tr><td> -SEM REGISTROS-</td></tr>");
            }
            // *** AQUI PRECISO DE UMA FUNÇÃO QUE JOGUE A PAGINA ATUAL PARA INDEX.HTML
        },
    });

In PHP I have the following condition:

        if ($intruso==1){
            //manda o usuario para INDEX.HTML
        }else{
            $sql = " SELECT * FROM tbl_cartao"
            $qryLista = mysqli_query($con, $sql);   
            while($resultado = mysqli_fetch_assoc($qryLista)){
                $vetor[] = array_map('utf8_encode', $resultado); 
            }   
            echo json_encode($vetor);
        }
    
asked by anonymous 11.09.2017 / 01:48

1 answer

1

You can use window.location for redirection in AJAX. See:

if(sua condicao aqui dentro){
    window.location = "index.html";
}

If you want to redirect in PHP, you can do this:

echo "<script>location.href='index.html';</script>"; 

Or use header :

header('Location: index.html');

The observation is that you have to check exactly where your index.html is, so that it is redirected.

    
11.09.2017 / 01:54