没有创建Jquery cookie

时间:2014-04-11 09:00:03

标签: javascript jquery arrays cookies plugins

您好我在我的head-tag中包含了jquery.cookie-plugin,现在我正在使用此片段来测试cookie:

$(document).ready(function() {
    $userSettings = array(
        "personal_information" = array(
            "name" => "name",
            "last_name" => "lastname"
        ),
        "extra_information" = array(
            "twitter" => "anyName",
            "facebook" => "anyName",
            "website" => "http://domain.com/",
            "programming_languages" => array("JavaScript", "PHP", "Java")
        )
   );
});
// Creating the JSON object
$jsonObject = json_encode($userSettings);

(function($){
        $(document).on('ready', function(){
            //A cookie by the name 'userSettings' now exists with a serialized copy of $userSettings
            $.cookies.set( 'userSettings', <?php echo $jsonObject; ?> );

            //A variable named 'userSettings' now holds the unserialized object, it should be identical to the PHP variable 'userSettings'
            var userSettings = $.cookies.get( 'userSettings' );

            // Do something with the values read from cookie
            console.log(userSettings);
        });
})(jQuery);

但我的控制台中没有日志,任何人都可以告诉我我做错了什么,因为我在我的控制台中得到了这个引用:

ReferenceError: invalid assignment left-hand side

$userSettings = array("personal_information" = array(

问候!!

3 个答案:

答案 0 :(得分:0)

试试这个

 <?php 
$userSettings = array(
    "personal_information" = array(
        "name" => "name",
        "last_name" => "lastname"
    ),
    "extra_information" = array(
        "twitter" => "anyName",
        "facebook" => "anyName",
        "website" => "http://domain.com/",
        "programming_languages" => array("JavaScript", "PHP", "Java")
    )
 );

  // Creating the JSON object

$jsonObject = json_encode($userSettings);
  ?>

<script type="text/javascript">
   (function($){
    $(document).on('ready', function(){
        //A cookie by the name 'userSettings' now exists with a serialized copy of $userSettings
        $.cookie.set( 'userSettings', <?php echo $jsonObject; ?> );

        //A variable named 'userSettings' now holds the unserialized object, it should be identical to the PHP variable 'userSettings'
        var userSettings = $.cookie.get( 'userSettings' );

        // Do something with the values read from cookie
        console.log(userSettings);
    });
 })(jQuery);

   </script>

答案 1 :(得分:0)

您必须改为使用$.cookie()

(function($){
    $(document).on('ready', function(){

        $.cookie( 'userSettings', '<?php echo $jsonObject; ?>' ); // change here

        var userSettings = $.cookie( 'userSettings' ); // here

        console.log(userSettings);
    });
})(jQuery);

答案 2 :(得分:0)

为什么不马上把它变成JSON对象:

var userSettings = [{
    "personal_information" : {
        "name" :"name",
        "last_name" : "lastname"
    }},
    {"extra_information" : {
        "twitter" : "anyName",
        "facebook" : "anyName",
        "website" : "http://domain.com/",
        "programming_languages" : ["JavaScript", "PHP", "Java"]
    }
}] 

@Jai是对的:您应该使用$.cookie()代替get()set()

$.cookie( 'userSettings', JSON.stringify(userSettings));