Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
symfony、jquery、JSON.parse:应为属性名_Jquery_Arrays_Json_Symfony - Fatal编程技术网

symfony、jquery、JSON.parse:应为属性名

symfony、jquery、JSON.parse:应为属性名,jquery,arrays,json,symfony,Jquery,Arrays,Json,Symfony,我对symfony、twig和jquery有问题 我想从数据库中获取联系人列表,然后在单击按钮时,必须将联系人列表电话添加到名为phone\u list的字段中 在我的控制器中,我有: $connection = $em->getConnection(); $statement = $connection->prepare("select * from contact where id_user=$id"); $statement->execute(); $contacts =

我对symfony、twig和jquery有问题

我想从数据库中获取联系人列表,然后在单击按钮时,必须将联系人列表电话添加到名为
phone\u list
的字段中

在我的控制器中,我有:

$connection = $em->getConnection();
$statement = $connection->prepare("select * from contact where id_user=$id");
$statement->execute();
$contacts = $statement->fetchAll();
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$jsonContent = $serializer->serialize($contacts, 'json');
在javascript区域中,我有以下代码:

if($(this).is(":checked")) {
       var contacts_json = $.parseJSON("{{ contacts|json_encode() }}");
       var points = [];
       var ii = 0;
       for (var i in contacts_json) {
           points[ii] = contacts_json[i]["id"];
           i++;
       }
       $('#destinationList').val(points.join(';'));
    }
    else {
        $('#destinationList').val("");
    }
});
我把
{{dump(contacts)}}
放在树枝上,结果是:

但当我尝试执行该函数时,控制台中出现错误

L'getPreventDefault()的利用率是最令人失望的。利用者«违约预防»拉普拉斯。 SyntaxError:JSON.parse:JSON数据的第1行第3列应为属性名或“}”


我建议您在查询中转义参数
$id
。此外,我建议您阅读有关PDO的文档,因为这里有一些额外的不需要的声明。。。为什么不改用
$jsonContent
变量呢?它已经序列化了。为什么要在Twig中再次序列化相同的数据?我用contentJson替换了$contacts,但是我应该如何在javascript代码中工作?如何将javascript集成到Twig中?在我看来,您正在解析字符串。由于json表示,您是否尝试过使用简单的引号
'{“p”:5}'