如何在JavaScript中删除PHP cookie?

时间:2016-07-18 13:50:44

标签: javascript php jquery ajax cookies

我在最近两小时内尝试过但未在JavaScript代码中删除PHP Cookie。

这是我的代码:

的index.html:

<a href="javascript:;" onclick="test();">Set Cookie</a>

function test(){
 $.ajax({
     url:'ajax.php',
     method: 'post',
     data: data,
     success: function(response){
        //success code here
     }
  });
}

if(getCookie('redirect')){
   removeCookie('redirect');
}

AJAX.php:

setcookie('redirect', 'Hello', time() + 60 * 60 * 24 * 1);

的script.js:

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function removeCookie(name) {
    alert(getCookie(name));
    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

我发布了我的所有代码。但我的cookie不会被删除。每当我刷新页面时,我都会收到Hello

0 个答案:

没有答案