jQuery cookie with JSON“未定义”不是一个函数

jQuery cookie with JSON "undefined is not a function"

本文关键字:未定义 函数 一个 with cookie JSON jQuery      更新时间:2023-09-26

我哪里做错了?

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;
}

在第一次调用时ok, cookie被设置,解析后在我的控制台上显示如下:

Object {id: 72} 

但是在第二次点击时,我不能推送另一个项目在products.push上获取"undefined is not a function"

products设置为具有单个对象的数组,而不是裸对象:

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