javascript 如何删除注销按钮上的cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17645330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:03:52  来源:igfitidea点击:

How to remove a cookie on logout button?

javascriptjquerycookies

提问by Lion789

I am having a hard time removing a cookie when the logout button is clicked. I currently have a logout button with the class logout. and this is the simple jquery line I am trying to put to remove the cookie but it is not working.

单击注销按钮时,我很难删除 cookie。我目前有一个带有类注销的注销按钮。这是我试图删除cookie的简单jquery行,但它不起作用。

$('button .logout').click(function () {
            $.cookie('userid', null);
       };

userid is the name of the cookie.

userid 是 cookie 的名称。

Oh I am using a signed cookie, not sure if that changes anything?

哦,我正在使用签名 cookie,不确定这是否会改变什么?

EDIT:

编辑:

Here is how I set the cookie:

这是我设置cookie的方法:

exports.loginPost = function(req, res, next) {
    console.log("here 1");
    passport.authenticate('local', function(err, user, info) {
        if (err) { return next(err) }
        if (!user) { 
            return res.render('loginError', {title: 'Weblio'}); }
        req.logIn(user, function(err) {
            if (err) { return next(err); }
            console.log(JSON.stringify(user));
            res.cookie('userid', user._id, { maxAge: 900000, signed: true });
            res.redirect('userProfile');
        });
  })(req, res, next);
};

And using a simple express - node js cookieParser with a secret in it.

并使用一个简单的 express - node js cookieParser,其中包含一个秘密。

采纳答案by Shawn31313

Give it negative time and just set the value to nothing.

给它负时间,然后将值设置为空。

Hmm, try:

嗯,试试:

$('button .logout').click(function () {
    $.cookie('userid', "", -1);
});