weixin_33749242 2017-02-15 17:36 采纳率: 0%
浏览 30

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.

  • 写回答

2条回答 默认 最新

  • hurriedly% 2017-02-15 17:48
    关注

    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";
            }
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样