简体   繁体   English

使用AJAX登出无法正常工作

[英]logout using AJAX is not working properly

i want to logout on clicking this button 我要注销点击此按钮

html input HTML输入

<input type="button" value="logout" id="top-btn-logout" onclick="test()"> 

my js file 我的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;
}?>

First use a proper get request using jquery ajax try 首先使用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;
            }
        });

then check.php file. 然后检查.php文件。 (you are combining conditions to check use both conditions separate) (您正在组合条件以分别使用两个条件进行检查)

<?php
if(isset($_GET['action'])  && $_GET['action'] == "logout" ){

    //$_SESSION = array();
    session_destroy();
    session_unset();  
    echo "logout";exit;
}?>
  if(isset($_GET['action']) == "logout" ){

This is incorrect because isset will return true and true != 'logout' so make it as 这是不正确的,因为isset将返回true和true!='logout',因此将其设置为

 if(isset($_GET['action'])){
   if($_GET['action']=="logout";
     //do whatever you wanna do
 }

You need to check for returned data from check.php file on success of ajax request . 成功执行ajax请求时,您需要检查check.php文件中返回的数据。 After that redirect the user to your login page. 之后,将用户重定向到您的登录页面。

Try this it will work : 试试这个将起作用:

JS File : 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 : check.php:

<?php
if(isset($_GET['action']) == "logout" ){

header("HTTP/1.1 401 Unauthorized");

}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM