Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 未创建Jquery cookie_Javascript_Jquery_Arrays_Cookies_Plugins - Fatal编程技术网

Javascript 未创建Jquery cookie

Javascript 未创建Jquery cookie,javascript,jquery,arrays,cookies,plugins,Javascript,Jquery,Arrays,Cookies,Plugins,大家好,我将jquery.cookie-plugin包含在我的head标记中,现在我使用这个片段来测试cookie: $(document).ready(function() { $userSettings = array( "personal_information" = array( "name" => "name", "last_name" => "lastname" ),

大家好,我将jquery.cookie-plugin包含在我的head标记中,现在我使用这个片段来测试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);
你好

试试这个

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

(函数($){
$(文档).on('ready',function(){
//名为“userSettings”的cookie现在存在,并带有$userSettings的序列化副本
$.cookie.set('userSettings',);
//名为“userSettings”的变量现在保存未序列化的对象,它应该与PHP变量“userSettings”相同
var userSettings=$.cookie.get('userSettings');
//对从cookie读取的值执行一些操作
console.log(用户设置);
});
})(jQuery);

您必须改用
$.cookie()

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

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

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

        console.log(userSettings);
    });
})(jQuery);
(函数($){
$(文档).on('ready',function(){
$.cookie('userSettings','');//在此处更改
var userSettings=$.cookie('userSettings');//此处
console.log(用户设置);
});
})(jQuery);

为什么不直接将其转换为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()


那么,这些答案是否有帮助?你设法解决了这个问题吗?请更新此SO项目;也许你可以检查其中一个答案为“正确”。。。
var userSettings = [{
    "personal_information" : {
        "name" :"name",
        "last_name" : "lastname"
    }},
    {"extra_information" : {
        "twitter" : "anyName",
        "facebook" : "anyName",
        "website" : "http://domain.com/",
        "programming_languages" : ["JavaScript", "PHP", "Java"]
    }
}] 
$.cookie( 'userSettings', JSON.stringify(userSettings));