$ .ajax通过PHP重定向后无法正常工作 [英] $.ajax post redirect via php not working

查看:70
本文介绍了$ .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;
}

这篇关于$ .ajax通过PHP重定向后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆