【问题标题】:header('Location: ..') is sending get request, and not redirectingheader('Location: ..') 正在发送获取请求,而不是重定向
【发布时间】:2015-01-25 12:31:12
【问题描述】:

我试图在登录后重定向用户,但不是重定向,而是发送 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 方法发送登录脚本请求。

提前谢谢你。

【问题讨论】:

  • 通常角度应用会加载一个新的角度路径,而不是加载一个全新的页面

标签: php angularjs redirect login header


【解决方案1】:

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

echo "../doc.php";

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

在 javascript 中进行重定向
location.href=data_received_from_post_request;

【讨论】:

  • 好的,所以问题是它被包裹在 Angular 的 ajax 方法中。我懂了。谢谢!
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
相关资源
最近更新 更多