简体   繁体   中英

Codeigniter getJSON not working in external Javascript file

I write some JS script directly at my header (I'm using codeigniter) and everything works correctly. But then I try to separate that script from header so I create JS file and put the script there. I call the JS file from header file but the getJSON function inside my script can not work properly. Here is how I call the external JS file from header :

<script type="text/javascript" src="<?php echo base_url('asset/js/charts/v_soaotc_daily.js')?>"></script>

Here is my full JS :

$(function () {

     $('#soaotc_daily_chart').highcharts({ 
    chart: {
        zoomType: 'xy'
    },
    title: {
        text: 'SOA_OTC Daily'
    },
    xAxis: [{
        categories: []
    }],
    yAxis: [{ // Primary yAxis
        labels: {
          //  format: '{value} Rs.',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        },
        title: {
            text: 'Amount',
            style: {
                color: Highcharts.getOptions().colors[1]
            }
        }
    }, { // Secondary yAxis
        title: {
            text: 'Population',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        labels: {
            //format: '{value} out of 100',
            style: {
                color: Highcharts.getOptions().colors[0]
            }
        },
        opposite: true
    }],
    tooltip: {
        shared: true
    },
    legend: {
        layout: 'vertical',
        align: 'left',
        x: 120,
        verticalAlign: 'top',
        y: 100,
        floating: true,
        backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'
    },
    series: [{
        name: 'Population',
        type: 'column',
        yAxis: 1,
        data: [],
        tooltip: {
            // valueSuffix: ' out of 100'
        },
        dataLabels: {
            enabled: true,
            // format: '{point.y:.0f}/100'
        }

    }, {
        name: 'Amount',
        type: 'spline',
        data: [],
        tooltip: {
            valueSuffix: ''
        }
      }]
     }, function(theChart){        

        $.getJSON("<?php echo base_url(); ?>soaotc/daily_json", function(json) {
            alert('sukses');
         theChart.xAxis[0].setCategories(json[0]['data']); 
         theChart.series[0].setData(json[1]['data'], false);
         theChart.series[1].setData(json[2]['data'], true);

        })
        .fail( function(d, textStatus, error) {
        console.error("getJSON failed, status: " + textStatus + ", error: "+error)
        });                               

    });     

        var theChart = $('#container').highcharts();   
});

FYI only the getJSON function which can not work. Here is what I found in console :

getJSON failed, status: parsererror, error: SyntaxError: Unexpected token D

If the getJSON success, it will return JSON data like this :

[{
  "name":"Date",
  "data":["27-OCT-14","28-OCT-14","29-OCT-14","30-OCT-14","31-OCT-14","01-NOV-14","02-NOV-14"]
 },
 {
  "name":"Population",
  "data":[6171,6990,6882,6889,6860,7619,6698]
 },
 {"name":"Amount",
  "data":[361154716.01,409210099.77,407191552.71,416366585.57,418588842.18,435168113.68,402163667.57]
}] 

Am i miss something in my code? or is there some config in codeigniter which allow us to call external JS file that i missed? Appreciate for your help.

Method 1 :

You need to remove backslashes if there any in the url. See here Same bug

Method 2 :

You can use $.post(url,data,function(json){...}) instead of getJson and use dataType : json with echo json_var from the controller page.

Problem has been solved in very simple way. I replace the URL in

$.getJSON("<?php echo base_url(); ?>soaotc/daily_json", function(json){..});

to

$.getJSON("daily_json", function(json){..});

Thank you.

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