并提前感谢。 我有这个问题,在插件中当我带ajax信息用于构建带有“Morris.js”的条形图时,轴的每隔一个条形图都没有显示信息,但是如果我直接将“json”写入代码它工作正常我的错误是什么?这是代码:
$(document).ready(function(){
var numberOfSeconds=30*1000; //the interval of refreshing the information of the calls per server
var x=getTheInfoAboutTheServers();
//The m_graph contains an information
//and build the graph itself later we update the data with the m_graph.setData() function
var m_graph= Morris.Bar({
element: 'my_chart',
data: x,
xkey: 'y',
ykeys: ['a'],
labels: ['Number of calls']
});
/**
* This Function get the number of calls per each server
* return a JSON object to the morris.js pluggin
*/
function getTheInfoAboutTheServers(){
var inf=null;
$.ajax({
async: false,
type: "POST",
url : $('#site_url').val()+"index.php/Login_controller/mw_iaa_rawGraphInfoClient",
dataType: "JSON",
success: function(data){
var dataArr=[];
for (key in data) {
if (!isNaN(key)) {
var obj = data[key];
var mySplitResult = obj['callserver'].split(".");
var calls=parseInt(obj['numOfCalls']);
dataArr.push({ y: String(mySplitResult[0]), a : calls});
}
}
inf=dataArr;
}
});
return inf;
}
答案 0 :(得分:0)
这是因为$ .ajax在函数getTheInfoAboutTheServers中异步执行。因此,此方法返回inf的值,即 null ,因为此时不执行ajax函数。
要解决此问题,您需要从ajax-success方法中调用另一个函数,将json作为参数传递,而不是将其赋值给变量