Cookie总是空的?

时间:2012-10-06 06:12:34

标签: javascript caching

我想检查cookie是否存在

问题是,如果我检查它总是返回null

if ($.cookie('cookieCreditDash') == null) {

   //CODE ALWAYS ENTER HERE... :(
            alert("no cookie");    
            $.getJSON(url, function(data) {
                saveCreditCookie(data);
            });
} else{
            alert("with cookie");
            var data =  JSON.parse($.cookie("cookieCreditDash"));
}

function saveCreditCookie(jsonData){
    var date = new Date();
    date.setTime(date.getTime() + (30 * 60 * 1000));
    $.cookie("cookieCreditDash", JSON.stringify(jsonData), {expires: date});
 }

WHYYY> ??

BTW,我使用了以下插件:

(function ($, document, undefined) {
        var pluses = /\+/g;
        function raw(s) {
            return s;
        }
        function decoded(s) {
            return decodeURIComponent(s.replace(pluses, ' '));
        }
        var config = $.cookie = function (key, value, options) {
            // write
            if (value !== undefined) {
                options = $.extend({}, config.defaults, options);
                if (value === null) {
                    options.expires = -1;
                }
                if (typeof options.expires === 'number') {
                    var days = options.expires, t = options.expires = new Date();
                    t.setDate(t.getDate() + days);
                }
                value = config.json ? JSON.stringify(value) : String(value);
                return (document.cookie = [
                    encodeURIComponent(key), '=', config.raw ? value : encodeURIComponent(value),
                    options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                    options.path    ? '; path=' + options.path : '',
                    options.domain  ? '; domain=' + options.domain : '',
                    options.secure  ? '; secure' : ''
                ].join(''));
            }

            // read
            var decode = config.raw ? raw : decoded;
            var cookies = document.cookie.split('; ');
            for (var i = 0, parts; (parts = cookies[i] && cookies[i].split('=')); i++) {
                if (decode(parts.shift()) === key) {
                    var cookie = decode(parts.join('='));
                    return config.json ? JSON.parse(cookie) : cookie;
                }
            }
            return null;
        };
        config.defaults = {};
        $.removeCookie = function (key, options) {
            if ($.cookie(key) !== null) {
                $.cookie(key, null, options);
                return true;
            }
            return false;
        };
    })(jQuery, document);

3 个答案:

答案 0 :(得分:1)

什么是$.cookie?一个jquery插件?

您是否尝试使用document.cookie来设置Cookie而不是$.cookie

https://developer.mozilla.org/en-US/docs/DOM/document.cookie

答案 1 :(得分:1)

正如我从文档jquery.cookie.js中看到的那样:expires是天数

if ($.cookie('cookieCreditDash') == null)
{
     $('div').text("no cookie");
     $.cookie("cookieCreditDash", 'test', {expires: 1});
} else{
     var data =  $.cookie("cookieCreditDash");
     $('div').text("with cookie: " + data);        
} 

http://jsfiddle.net/HeGhf/

答案 2 :(得分:1)

看起来您正在使用jQuery cookie plugin

除了Cookie过期问题,您可能还需要检查Cookie路径,使其可用于域中的所有路径。您可以通过编写以下代码来完成此操作,

$.cookie("cookieCreditDash", "Value", { path: '/' });

如果没有{path: '/'},Cookie路径将仅限于当前路径级别。