我和ajax请求codeigniter函数,我只是像这样echo json_encode($array)
回显结果数组。我想要做的是将ajax的返回值作为一个数组捕获,我将在图表中使用(我使用Highcharts)作为一个系列。但是这段代码没有任何反应:
var data2 = [];
url = base_url+"/codeigniter/index.php/AssistanceMonitoringModule/assistanceMonitoring/getSeries";
$.ajax(
{
type: "GET",
url: url,
data: "query="+query,
success: function(data){
alert(data);
//data2 = data;
},
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert("XHR:"+xhr.status+"Error:"+thrownError);
}
});
这是我在HIghchart中的称呼方式:
$('#columnChart').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['May 01', 'May 02', 'May 03', 'May 04', 'May 05', 'May 06', 'May 07', 'May 08', 'May 09', 'May 10', 'May 11', 'May 12', 'May 13', 'May 14','May 15','May 16', 'May 17', 'May 18', 'May 19', 'May 20']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: data2
});
我使用data2作为数组,因为我说没有任何反应。 :)感谢您的帮助。
答案 0 :(得分:2)
您可以定义新函数来初始化并在onSuccess ajax上调用highcharts
插件,
试试这种方式
$.ajax(
{
type: "GET",
url: url,
data: "query="+query,
success: function(data){
//alert(data);
//data2 = data;
callHighChart(data);
},
dataType: "json",
error: function (xhr, ajaxOptions, thrownError) {
alert("XHR:"+xhr.status+"Error:"+thrownError);
}
});
function callHighChart(data){
$('#columnChart').highcharts({
chart: {
type: 'column'
},
title: {
text: 'Fruit Consumption'
},
xAxis: {
categories: ['May 01', 'May 02', 'May 03', 'May 04', 'May 05', 'May 06', 'May 07', 'May 08', 'May 09', 'May 10', 'May 11', 'May 12', 'May 13', 'May 14','May 15','May 16', 'May 17', 'May 18', 'May 19', 'May 20']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: data // ajax response;
});
}