2014-11-24 131 views
0

我直接在我的头文件(我正在使用codeigniter)编写一些JS脚本,并且一切正常。但后来我尝试将该脚本从头文件中分离出来,以便创建JS文件并将脚本放在那里。我从头文件调用JS文件,但脚本中的getJSON函数无法正常工作。 这是我如何调用从头外部JS文件:Codeigniter getJSON无法在外部Javascript文件中工作

<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(); 
}); 

FYI只的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文件?感谢您的帮助。

+0

向我们展示您从<?php echo base_url()返回的响应; ?> soaotc/daily_json' – 2014-11-24 06:49:35

+0

@CerlinBoss问题已更新 – 2014-11-24 06:59:42

+0

你检查了网络的请求和响应? – 2014-11-24 07:02:56

回答

0

方法1

您需要删除反斜杠,如果有任何在URL中。 See here Same bug

方法2

可以使用$.post(url,data,function(json){...})代替的getJSON和使用dataType : json从控制器页面回声json_var。

+0

你会告诉我在我的代码中删除反斜杠的例子吗? – 2014-11-24 07:13:51

+0

https://bitbucket.org/cloudengine/cloudengine/commits/89fea603238c1f7262 – Riad 2014-11-24 07:20:18

+0

但是,您可以尝试在数据中添加''':[6171,6990,6882,6889,6860,7619,6698]'行?我怀疑'''字符串应该在那里解析json成功。 – Riad 2014-11-24 07:21:22

0

问题已经以非常简单的方式解决了。我更换

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

网址

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

谢谢。

相关问题