2013-08-01 37 views
0

在此程序中,我製作了一個標題爲google.co.in的PHP文件。讓我們來看看代碼PHP中的標題不起作用

<?php 
session_start(); 
$status=$_POST['input']; 

    if (isset($status)) { 

     header('Location: http://google.co.in/'); 
    } 

?> 

,我有一個有另一個文件的javascript,讓我們看看到

<?php 
session_start(); 
echo time(); 
?> 

<html> 
<head> 
    <title>my app</title> 
    <script type="text/javascript" src="jquery-2.0.2.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function(){ 
      $(this).mousemove(function(){ 

       $.post('logout.php',{input: 1}); 
       //window.location.href="logout.php" 
      }); 
     }); 

    </script> 
</head> 
<body> 
    <h2>we are on the main_session</h2> 
</body> 
</html> 

現在的問題是,當我在我的本地運行此我沒有被重定向到google.co.in而是在螢火蟲中顯示302錯誤。但是,當我使用我在代碼中評論的window.location.href語法,而不是我被重定向到google.co.in時。請告訴我這背後有什麼問題。

如果你想
+0

您已經評價重定向代碼 – Suleman

+0

是.....我曾評論它 –

+0

請取消註釋它重定向成功 – Suleman

回答

0

重定向使用
window.location.href="logout.php"或者乾脆window.location="logout.php"

,而不是$.post()因爲後檢索目標網頁的內容,而window.location的重定向你!

0

問題是$.post是一種AJAX方法而不是重定向技術。很明顯,$.post用於需要從另一頁獲取任何內容而不重定向的情況。所以解決方案是使用window.location.href

0

試試這個

SCRIPT:

$.post('logout.php',{input: 1} , function(res){ 
    location.href="logout.php"; 
}); 

php文件

<?php 
session_start(); 
$status=$_POST['input']; 

    if (isset($status)) { 
     // destroy session or cookies 
     // return true 
    } 

?> 

希望這將有助於

0

我不認爲使用Ajax方法重定向的工作。由於您正在調用$ .post()ajax方法,因此會發送XHR請求並等待服務器響應。更好的解決方案是在Java腳本中使用window.location。

請參考下面的帖子在這裏更多的細節 PHP header() called via AJAX not working properly