js-cookie:Cookies.get(' cookieName')返回undefined

时间:2015-12-20 15:31:40

标签: javascript jquery cookies js-cookie

我使用this library作为JQuery插件我正在编码...我在cookie中保存了用户以这种方式创建的特定数据:

// Update cookies checks if the cookie esists.
// If cookie exists => push data {myData:[obj1[, obj2, [...], objN]]}
// If cookie doesn't exists => create cookie with {myData:[obj1]}
function _updateCookie(name, cookie, data) {
    // Check if cookie exists
    var cookies = {myData:[]};

    // _getCookie(name) { return Cookies.get(name) }
    if (_getCookie(name) != undefined) {
        cookies = _getCookie(name);
    }

    cookies.reminders.push(cookie);

    Cookies.set(name, cookies, data);
}

数据:

var data = {
    expires: 1,
    path: '/',
    domain: '',
    secure: true
}

COOKIE:

var cookie = {
    myData: [
        1: myObject1,
        2: myObject2,
        // [...]
        n: myObjectN
    ],
}

当我致电_getCookie(name)时,它始终会返回undefined。我还尝试将Cookie列为:

console.log(Cookies.get());
// log: Object {_ga: "GA1.1.402426237.1450622600"}

但如果我看一下chrome:// settings / cookies我看到:

本地主机上的2个cookie [_ga],[myCookie]

有什么建议我做错了吗?

修改

当我使用此值<{p>}调用Cookie.set();时,我发现问题就出现了

Cookies.set('myCookie', {[expires: ...], path: '', domain: ''});

如果我打电话

Cookies.set('myCookie'{[expires: ...]});

我可以毫无问题地获得cookie。

0 个答案:

没有答案