简体   繁体   中英

Unwanted Json appers in html

I have a problem in my code which I can not see where the problem comes. When I get the JSON data in JS, shows the structure of the array on the Web page, as shown in the picture below. 在此处输入图片说明

This is an excerpt from my JS code

var proj = document.getElementById('ProjetosSelect').value;
$.ajax({
    url: 'CRM files/TSread.php',
    type: "POST",
    data: ({ProjetosSelect: proj}),
    dataType: "json",
    complete:function(data) 
    {
        ProjetoHoras = data.responseJSON.total;
        ProjetoGastas = data.responseJSON.gastas;
        var PorGastar = ProjetoHoras - ProjetoGastas;
        $('#graficos').highcharts({
            chart: {
                type: 'column',
                margin: 75,
                options3d: {
                    enabled: true,
                    alpha: 10,
                    beta: 25,
                    depth: 70
                }
            },
            title: {
                text: 'Horas Contratadas'
            },
            plotOptions: {
                column: {
                    depth: 25
                }
            },
            xAxis: {
                categories: 
                 ['Horas Totails','Horas Gastas','Horas por Gastar']   

            },
            yAxis: {
                title: {
                    text: null
                }
            },
            series: [{
                name: 'Horas',
                data: [ProjetoHoras, ProjetoGastas, PorGastar]
            }]
        });

    }

});

and this is an excerpt from my PHP code

$output = array('total'=>(float)$BHoras[1], 'gastas'=>(float)$BHoras[2]);
echo json_encode($output);

it is possible to eliminate " {"total":0,"gastas":0} " from web page?

Content is echoed for both the web page and the ajax.

Each of them should have a distinct url. The PHP page which echos the array JSON should be in a different url from the webpage itself.

Otherwise, you can use a parameter to distinguish them like so:

In Javascript add:

data: ({ProjetosSelect: proj,action:'json'}),

In php:

    if( ! empty($_POST['action']) && $_POST['action] == 'json'){
      $output = array('total'=>(float)$BHoras[1], 'gastas'=>  (float)$BHoras[2]);
      echo json_encode($output);
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM