【发布时间】: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文件?感谢您的帮助。
【问题讨论】:
-
向我们展示您从
<?php echo base_url(); ?>soaotc/daily_json返回的回复 -
@CerlinBoss 问题已更新
-
您是否检查过网络的请求和响应?
-
你检查过base_url()返回的值吗;
-
感谢您的建议@CerlinBoss。我通过检查网络找到了解决问题的方法。我会更新答案
标签: javascript php jquery codeigniter getjson