【问题标题】:Codeigniter getJSON not working in external Javascript fileCodeigniter getJSON 在外部 Javascript 文件中不起作用
【发布时间】:2014-11-24 16:05:59
【问题描述】:

我直接在我的标题处编写了一些 JS 脚本(我正在使用 codeigniter)并且一切正常。但是后来我尝试将该脚本与标头分开,因此我创建了 JS 文件并将脚本放在那里。我从头文件中调用 JS 文件,但我的脚本中的 getJSON 函数无法正常工作。 以下是我如何从标题中调用外部 JS 文件:

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

这是我的完整 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();   
});

仅供参考,只有 getJSON 函数不起作用。这是我在控制台中找到的:

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

如果 getJSON 成功,它将返回如下 JSON 数据:

[{
  "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]
}] 

我是否错过了我的代码中的某些内容?或者codeigniter中是否有一些配置允许我们调用我错过的外部JS文件?感谢您的帮助。

【问题讨论】:

  • 向我们展示您从&lt;?php echo base_url(); ?&gt;soaotc/daily_json返回的回复
  • @CerlinBoss 问题已更新
  • 您是否检查过网络的请求和响应?
  • 你检查过base_url()返回的值吗;
  • 感谢您的建议@CerlinBoss。我通过检查网络找到了解决问题的方法。我会更新答案

标签: javascript php jquery codeigniter getjson


【解决方案1】:

方法一

如果网址中有反斜杠,您需要删除。 See here Same bug

方法二

您可以使用 $.post(url,data,function(json){...}) 代替 getJson 并使用 dataType : json 和控制器页面中的 echo json_var。

【讨论】:

  • 你能告诉我在我的代码中删除反斜杠的例子吗?
  • 但是请您尝试在"data":[6171,6990,6882,6889,6860,7619,6698] 行中添加" 吗?我怀疑" 字符串应该在那里成功解析json。
  • 感谢您的帮助,问题已通过替换getJSON函数中的URL解决。
【解决方案2】:

问题已以非常简单的方式解决。我将网址替换为

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

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

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多