【问题标题】:How to fix SyntaxError: Unexpected token &如何修复 SyntaxError: Unexpected token &
【发布时间】:2016-09-01 21:40:18
【问题描述】:

我将根据 json_encode 函数编码的 php 发送到我想要绘制饼图类型的 plantilla.twig 并且浏览器在控制台中向我显示以下错误 uncaught SyntaxError: Unexpected token & ¨ 并且我将行以下数据:[["中间17.5寸",2220]]

控制器类

public function tiempoXEtapaAction()
{

    $em = $this->getDoctrine()->getManager();
    $servicepozo = $this->get('service.pozo');
    $pozoActual = $servicepozo->getPozoActual();
    $idpozo = $servicepozo->getPozoActual()->getId();

    $activityCollections = $em->getRepository('ActividadBundle:ActividadDiariaCollection')->findBy(array('pozo' => $idpozo), array('id' => 'ASC'));
    $intervalos = $em->getRepository('IntervaloBundle:Intervalo')->findBy(array('pozo' => $idpozo), array('id' => 'ASC'));
    $actividades = $em->getRepository('ActividadBundle:ActividadDiaria')->findBy(array('pozo' => $idpozo), array('id' => 'ASC'));


    $tiempoXIntervalo = array();

    foreach ($activityCollections as $activityCollection) //Dias
    {
        $fechaActCole = $activityCollection->getFecha();

        foreach ($intervalos as $intervalo) //Intervalos
        {
            if ($intervalo->getFechaInicio() <= $fechaActCole && $intervalo->getFechaFinal() >= $fechaActCole) {
                $temp = array();
                $temp[] = $intervalo.'';


                foreach ($actividades as $actividad) //Actividades
                {

                    if ($actividad->getCollection()->getId() == $activityCollection->getId()) {
                        $duracion[] = $actividad->getDuracion();
                    }

                }
                $temp[] = array_sum($duracion);

            }

        }
        $tiempoXIntervalo[] = $temp;


    }

     return array('tiempoXIntervalo'=> (json_encode($tiempoXIntervalo)));

模板.Twig

            title: {
                text: 'Distribución de tiempo por Intervalo'
            },
            xAxis: {
                reversed: false,
                title: {
                    enabled: true,
                    text: 'Dias'
                },
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                },
                maxPadding: 0.05,
                showLastLabel: true,
                allowDecimals: false,
                min:0,
            },
            yAxis: {
                max:0,
                startOnTick: false,
                title: {
                    text: 'Profundidad [m]'
                },
                labels: {
                    formatter: function () {
                        return this.value;
                    }
                },
                lineWidth: 2,

            },
            legend: {
                enabled: true
            },

            tooltip: {
                pointFormat: '{series.name}: <b>{point.y}h ({point.percentage:.1f}%)</b>'
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: false
                    },
                    showInLegend: true
                }
            },
            series: [{
                data:{{tiempoXIntervalo}}
            }]
        });
    });

</script>

【问题讨论】:

    标签: json twig


    【解决方案1】:

    感谢费尔南多,但您的解决方案不适用于 mi。 显然问题是 Twig 自动转义变量。我尝试使用 Twig 的原始过滤器来跳过自动转义,它适用于 mi。解决办法在这里

    数据:$.parseJSON('{{ tiempoXIntervalo | raw }}')

    【讨论】:

    【解决方案2】:

    你应该检查如何convert HTML entities 此外,&amp; quot; 中还有一个额外的空间 应该是&amp;quot;

    json_encode 需要 " 所以它应该是 ["Intermediate 17.5 inches",2220]

    【讨论】:

    • 请在此处总结链接
    • $a = "&amp;quot; Intermediate 17.5 inches &amp;quot;, 2220"; $b = html_entity_decode($a); echo $b; // "Intermediate 17.5 inches",2220
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2021-03-22
    • 2021-08-13
    • 2016-11-18
    • 1970-01-01
    • 2020-01-06
    • 2012-09-22
    相关资源
    最近更新 更多