如何在javascript中删除和重置cookie? [英] How to delete and reset a cookie in javascript?

查看:506
本文介绍了如何在javascript中删除和重置cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"fc_vid=visitor1089537543049; _gat=1; Email Id=; Password=; API={"access_token":"fca10765-e1b0-42bf-bc11-47d4e436533b","token_type":"bearer","refresh_token":"969b3993-983c-4308-8542-bc0b0cd861ac","expires_in":429373,"scope":"read write"}; pnctest=1; fc_g=%7B%22session_geo%22%3A%22%7B%5C%22locale%5C%22%3A%7B%5C%22country%5C%22%3A%5C%22us%5C%22%2C%5C%22lang%5C%22%3A%5C%22en%5C%22%7D%2C%5C%22current_session%5C%22%3A%7B%5C%22url%5C%22%3A%5C%22http%3A%2F%2Flocalhost%3A5567%2FHome%2FIndex%5C%22%7D%2C%5C%22browser%5C%22%3A%7B%5C%22browser%5C%22%3A%5C%22Chrome%5C%22%2C%5C%22version%5C%22%3A43%2C%5C%22os%5C%22%3A%5C%22Windows%5C%22%7D%2C%5C%22device%5C%22%3A%7B%5C%22is_tablet%5C%22%3Afalse%2C%5C%22is_phone%5C%22%3Afalse%2C%5C%22is_mobile%5C%22%3Afalse%7D%7D%22%7D; _ga=GA1.1.1692099365.1433096280"

我试图删除API cookie并将其重置为一个新的对象,但是当我为它添加另一个API cookie。

I am trying to remove the API cookie and reset it to a new object however when i do that it adds another API cookie for me.

下面是我的代码,我使用cookies.js从这链接

Below is my code i am using cookies.js from this link

   var dataPromise = get_api_token_refresh_token(refresh_token).done(handleData).fail(failHandler);
    dataPromise.success(function (api_token) {
   var something = docCookies.removeItem('API');
        console.log(something);
        docCookies.setItem("API", JSON.stringify(api_token));
   });

我的情况发生了什么:

"API=%7B%22access_token%22%3A%22f7b87fc2-f2d5-43e8-a6d2-cc4a89da7a50%22%2C%22token_type%22%3A%22bearer%22%2C%22refresh_token%22%3A%223630934c-93c3-4497-abfb-9022428fce4c%22%2C%22expires_in%22%3A431999%2C%22scope%22%3A%22read%20write%22%7D; fc_vid=visitor1089537543049; _gat=1; Email Id=; Password=; API={"access_token":"fca10765-e1b0-42bf-bc11-47d4e436533b","token_type":"bearer","refresh_token":"969b3993-983c-4308-8542-bc0b0cd861ac","expires_in":429373,"scope":"read write"}; pnctest=1; fc_g=%7B%22session_geo%22%3A%22%7B%5C%22locale%5C%22%3A%7B%5C%22country%5C%22%3A%5C%22us%5C%22%2C%5C%22lang%5C%22%3A%5C%22en%5C%22%7D%2C%5C%22current_session%5C%22%3A%7B%5C%22url%5C%22%3A%5C%22http%3A%2F%2Flocalhost%3A5567%2FHome%2FIndex%5C%22%7D%2C%5C%22browser%5C%22%3A%7B%5C%22browser%5C%22%3A%5C%22Chrome%5C%22%2C%5C%22version%5C%22%3A43%2C%5C%22os%5C%22%3A%5C%22Windows%5C%22%7D%2C%5C%22device%5C%22%3A%7B%5C%22is_tablet%5C%22%3Afalse%2C%5C%22is_phone%5C%22%3Afalse%2C%5C%22is_mobile%5C%22%3Afalse%7D%7D%22%7D; _ga=GA1.1.1692099365.1433096280"

注意:Cookie是在asp.net mvc代码

NOTE: The cookies are being set in the asp.net mvc code

string token_string = "";
                if (loginResult.User != null)
                {
                    //make a call to the REST api authentication method and get accesstokens
                    token_string = OAuthHelper.getTokenFromAPIServer(api_auth_username, api_auth_password);
                    if (collection["Check"] != null)
                    {
                        check = collection["Check"];

                        if (check == "on")
                        {
                            FormsAuthentication.SetAuthCookie(loginResult.User.Name, true);
                            Response.Cookies[Constants.Cookies.USERNAME].Value = loginResult.User.Email;
                            Response.Cookies[Constants.Cookies.PWD].Value = savePassword;//loginResult.User.Password;
                            Response.Cookies[Constants.Cookies.USERNAME].Expires = DateTime.Now.AddDays(10);
                            Response.Cookies[Constants.Cookies.PWD].Expires = DateTime.Now.AddDays(10);


                        }
                    }
                    else
                    {
                        FormsAuthentication.SetAuthCookie(loginResult.User.Name, false);
                        Response.Cookies[Constants.Cookies.USERNAME].Value = string.Empty;
                        Response.Cookies[Constants.Cookies.PWD].Value = string.Empty;
                    }
                    Response.Cookies[Constants.Cookies.API].Value = token_string;


推荐答案

您可以使用 jquery.cookie 插件。非常易于使用:

you can use jquery.cookie plugin. It's very easy to use:

删除Cookie:

// Returns true when cookie was successfully deleted, otherwise false
$.removeCookie('name'); // => true
$.removeCookie('nothing'); // => false

// Need to use the same attributes (path, domain) as what the cookie was written with
$.cookie('name', 'value', { path: '/' });
// This won't work!
$.removeCookie('name'); // => false
// This will work!
$.removeCookie('name', { path: '/' }); // => true

创建会话cookie:

Create session cookie:

$.cookie('name', 'value');

建立到期的Cookie,7天后:

Create expiring cookie, 7 days from then:

$.cookie('name', 'value', { expires: 7 });

创建有效的cookie,整个网站都有效:

Create expiring cookie, valid across entire site:

$.cookie('name', 'value', { expires: 7, path: '/' });

阅读cookie:

$.cookie('name'); // => "value"
$.cookie('nothing'); // => undefined

阅读所有可用的Cookie:

Read all available cookies:

$.cookie(); // => { "name": "value" }

这篇关于如何在javascript中删除和重置cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆