2014-11-24 132 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){..}); 

謝謝。

相關問題