PHP AJAX重定向。 [帮帮我]

I have a form that works in a modal way for registration / login. The part of the register is working normally. But the login part has a redirect problem ...

I am using this code below for AJAX. I have done a lot of research on everything I read about windows.location, but I do not know how to do it exactly.

AJAX:

 function logar(){

    $.ajax({
        method: "post",
        url: "validalogin.php",
        data: $("#login-form").serialize(),
    success: function(data){

               alert(data);
    }

});
}

PHP: I make the checks that I think necessary before and after I try to create a session through a result in the bank / data that the user typed ...

if ($result >1) {
    session_name('nacional');
    session_start();
    $_SESSION['cpf']=$cpf;
    $_SESSION['senha']=$senha;


    header("Location: my_folder/index.php");

}
  • NOTE: The header tag is not targeting. Do I have to do something the way the server returns an "ok" for ajax and it changes the targeting? If so how to proceed?

Thanks for listening.

You should send a JSON response to your JavaScript instead of returning a header. The header redirects behind the scenes. Do something like this in your PHP:

return json_encode(['success' => true]);

Then, catch it in your success method of AJAX. You also need to tell your AJAX to expect JSON. Put that after your method and url parameters, like so:

function logar() {
    $.ajax({
        method: "post",
        url: "validalogin.php",
        data: $("#login-form").serialize(),
        dataType: 'json'
    })
    .success(function(data){
        if (data.success == true) {
            alert(data);
            window.location.href = "folder/index.php";
        }
    });
}

I have not yet been able to make the form work correctly. I will post below how I get the code.

FORM: I had to send via image due to the organization of the code

AJAX ( It's on the same page as the form.):

 function logar(){

    $.ajax({
        method: "post",
        url: "validalogin.php",
        data: $("#login-form").serialize(),
        dataType: 'json',
    success: function(data){

        if (data.success == true) {
            alert(data);
            window.location.href = "folder/index.php";
            }
        }

});
}

PHP: (I skipped a part of the code above, but it will not affect the operation here)

        $sql = "SELECT * FROM aluno_login WHERE cpf = '$cpf' and senha ='$senha'  ";

            $result = mysql_query($sql, $conexao);
                while($linha = mysql_fetch_row($result)) { 

                    if ($result <1){
                        unset ($_SESSION['cpf']);
                        unset ($_SESSION['senha']);
                        session_destroy();
                        //echo "Algo deu errado, tente novamente !";
                        //return json_encode('Error' => false);

                    }
                    if ($result >1) {
                        session_name('nacional');
                        session_start();
                        $_SESSION['cpf']=$cpf;
                        $_SESSION['senha']=$senha;
                        return json_encode('success' => true);





                    }

Should this work?