【问题标题】:Jquery cookie not created未创建 Jquery cookie
【发布时间】:2014-04-11 19:09:19
【问题描述】:

您好,我在 head-tag 中包含了 jquery.cookie-plugin,现在我正在使用这个 sn-p 来测试 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(

你好!!

【问题讨论】:

  • 那么,任何答案都有帮助吗?你设法解决了这个问题吗?请更新此 SO 项;也许您可以检查 1 个答案为“正确”...

标签: javascript jquery arrays cookies plugins


【解决方案1】:

试试这个

 <?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>

【讨论】:

    【解决方案2】:

    您必须改用$.cookie()

    (function($){
        $(document).on('ready', function(){
    
            $.cookie( 'userSettings', '<?php echo $jsonObject; ?>' ); // change here
    
            var userSettings = $.cookie( 'userSettings' ); // here
    
            console.log(userSettings);
        });
    })(jQuery);
    

    【讨论】:

      【解决方案3】:

      为什么不直接把它变成一个 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));
      

      【讨论】:

        猜你喜欢
        • 2017-06-04
        • 2021-03-28
        • 1970-01-01
        • 2013-08-19
        • 2011-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-06
        相关资源
        最近更新 更多