简体   繁体   中英

jQuery cookie with JSON “undefined is not a function”

What am i doing wrong here ?

function add_to_compare(product_id){
    products = getCookie("compare_list");
    if(typeof products != "undefined" && products){
        products = jQuery.cookie( 'compare_list' );
        products = jQuery.parseJSON(products);
        console.log(products);
        new_product = {"id":product_id};
        products.push(new_product);
    }else{
        products = {"id":product_id};
        jQuery.cookie("compare_list",JSON.stringify(products));
    }
    return false;
}

On the first call its ok , the cookie is set and after parsing it shows like that on my console:

Object {id: 72} 

But on the second click I cannot push another item Getting "undefined is not a function" on products.push

以单个对象而不是裸对象的数组形式开始products

products = [ {"id":product_id} ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM