PHP标头位置admin.php不重定向,但页面已下载

时间:2015-04-24 22:00:14

标签: php jquery ajax

console

我尝试在成功登录后重定向,代码如下:

Name

我可以在浏览器中看到admin.php已下载。

用p调用php:

//if success (if there is a row)
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();    
// make the server ready for a session
session_start();
$_SESSION['userId'] = $row['user_id'];
$_SESSION['first_name'] = $row['first_name'];
$_SESSION['user_role'] = $row['user_role'];
$_SESSION['status'] = "ok";
session_write_close();


//get user_role for proper redirection
$iUserRole = $row['user_role'];
if($iUserRole==0){

header('Location: ../admin.php');  
exit();
} else if($iUserRole==1){

  header('Location: ../user.php');
  exit();
} else if($iUserRole==2){

  header('Location: ../partner.php');

  exit();
}

并且console.log(data)记录整个html页面html / text。

1 个答案:

答案 0 :(得分:0)

您无法从ajax调用重定向页面。在这种情况下,您需要将url传递给您ajax响应,然后使用javascript重定向页面

PHP

//if success (if there is a row)
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();    
// make the server ready for a session
session_start();
$_SESSION['userId'] = $row['user_id'];
$_SESSION['first_name'] = $row['first_name'];
$_SESSION['user_role'] = $row['user_role'];
$_SESSION['status'] = "ok";
session_write_close();


//get user_role for proper redirection
$iUserRole = $row['user_role'];
if($iUserRole==0){

echo "{Full URL}";  
exit();
} else if($iUserRole==1){

  echo "{Full URL}";
  exit();
} else if($iUserRole==2){

  echo "{Full URL}";

  exit();
}

的Javascript

// similar behavior as an HTTP redirect
window.location.replace("{URL}");

// similar behavior as clicking on a link
window.location.href = "{URL}";