header('Location: .') 正在发送 get 请求,而不是重定向


header('Location: ..') is sending get request, and not redirecting

我正在尝试在登录后重定向用户,但不是重定向,而是发送GET请求并且页面保持不变。我想避免从前端重定向,因为我必须为会话设置一些参数。

这是登录脚本:

require_once("../data/db.php");
$input = json_decode(file_get_contents("php://input"));
$user = $input->user;
$pass = $input->pass;
$db = new DB();
$result = $db->read($user, $pass);
switch($result[0]->mode)
    case "patient":
        header("Location: ../patient.php");
        break;
    case "doc":
        header("Location: ../doc.php");
        break;
    default:
        header("Location: ../index.php?error=" . $result);
        break;
};

控制台输出:

GET http://localhost/patient.php                     [HTTP/1.1 200 OK 3ms]

由于在调用 header() 函数之前没有输出,我不确定是什么导致了问题。登录脚本请求由 $http.post() angularJS 方法发送。

提前谢谢你。

由于 HTTP 请求是使用 javascript 中的某个 post() 方法发送的,因此它不会重定向您的网页。您需要更改 PHP 登录脚本,以仅回显 URL,例如:

echo "../doc.php";

然后在客户端使用 JavaScript 重定向浏览器。我不熟悉 angularJS 中的 $http.post(),但您需要在某些成功函数中执行此操作(一旦 .post() 成功后调用),并使用 JavaScript 进行重定向

location.href=data_received_from_post_request;