[英]logout using AJAX is not working properly
我要注銷點擊此按鈕
HTML輸入
<input type="button" value="logout" id="top-btn-logout" onclick="test()">
我的js文件
function test()
{
$("#top-btn-logout").click(function() {
$.ajax({
url: 'http://localhost/New%20folder/vigcheck.php?action=logout',
success: function(data){
alert(data);
//location.reload();
//window.location.href = data;
}
});
});
}
check.php file.
<?php
if(isset($_GET['action']) == "logout" ){
//$_SESSION = array();
session_destroy();
session_unset();
echo "logout";exit;
}?>
首先使用jquery ajax try使用正確的get請求
$.ajax({
url: 'http://localhost/New%20folder/vigcheck.php',
type: 'get',
data:{action:'logout'},
success: function(data){
alert(data);
//location.reload();
//window.location.href = data;
}
});
然后檢查.php文件。 (您正在組合條件以分別使用兩個條件進行檢查)
<?php
if(isset($_GET['action']) && $_GET['action'] == "logout" ){
//$_SESSION = array();
session_destroy();
session_unset();
echo "logout";exit;
}?>
if(isset($_GET['action']) == "logout" ){
這是不正確的,因為isset將返回true和true!='logout',因此將其設置為
if(isset($_GET['action'])){
if($_GET['action']=="logout";
//do whatever you wanna do
}
成功執行ajax請求時,您需要檢查check.php文件中返回的數據。 之后,將用戶重定向到您的登錄頁面。
試試這個將起作用:
JS文件:
function test()
{
$("#top-btn-logout").click(function() {
$.ajax({
url: 'http://localhost/New%20folder/vigcheck.php?action=logout',
success: function(data){
alert(data);
//location.reload();
//window.location.href = data;
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (XMLHttpRequest.status === 401) {
location.href = 'index.php';
}
}
});
});
}
check.php:
<?php
if(isset($_GET['action']) == "logout" ){
header("HTTP/1.1 401 Unauthorized");
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.