我对javascript不太满意,下面是我想从JSON获取变量并将日期放在图表上作为标签的代码。下面是代码
function getdate(num){
$.ajax({
url: 'http://81.12.18.111/fibarobiz/showjson.php',
data: "device=41",
dataType: 'json',
success: function(data)
{
var aMon= data.date[num];
// var aMon = '2/18';
return aMon; <--no value to return
// var pmvalue =data.properties.value;
// $('#pm25v').append( pmvalue );
}
});
}
$(function() {
"use strict";
var Mon=getdate(0); <-- outcome always undefined
// ==============================================================
// Revenue
// ==============================================================
var ctx = document.getElementById('revenue').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
//labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
labels: [Mon, 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
datasets: [{
label: 'Current Week',
data: [12, 19, 3, 17, 6, 3, 7],
backgroundColor: "rgba(89, 105, 255,0.5)",
borderColor: "rgba(89, 105, 255,0.7)",
borderWidth: 2
}
......
无论我为变量“ aMon”使用静态值,都不能返回任何值。我认为它不能在JS文件中运行Ajax吗?请帮忙!!非常感谢!
威尔逊