$ .ajax通过PHP重定向后无法正常工作

问题描述:

我正在尝试通过$ .ajax帖子上的php重定向,但似乎无法正常工作

I am trying to redirect via php on $.ajax post but it seems be not working

这是ajax

$("#customer_logout_link").click(function() {
    $.ajax({
        type: "POST",
        data: {
            var: 'value'
        },
        dataType: 'text',
        success: function(data) {

        }
    });
});

这是php代码

<?php

if (isset($_POST["var"]))
{
    header("location: accounts_login.php");
    exit;
}
?>

ajax命中了php代码,并成功显示了响应.但我想从php重定向.我想念的是什么?

ajax hit the php code and response also shown in success . but i want to redirect from php . what i am missing ?

您无法执行此操作,因为重定向将完成到AJAX请求中.但是您可以检测到PHP中的某些内容以在PHP中进行重定向.

You cannot do this because the redirection will be done into the AJAX request. But you could detect something from PHP to redirect in PHP.

示例:

$("#customer_logout_link").click(function(){
   $.ajax({
      type: "POST",
      data: {var:'value'},
      dataType: 'text',
      success:function(data){
        if (data.indexOf('Location:') === 0) {
                window.location.href = data.substr(10);
            }
      }
    });
});

在您的PHP中:

if (isset($_POST["var"]))
{

    echo "Location: accounts_login.php";
    exit;
}