-1

并提前感谢。我有这个问题,在插件中,当我使用 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;

 }
4

1 回答 1

0

这是因为 $.ajax 在函数 getTheInfoAboutTheServers 中异步执行。因此,该方法返回inf 的值,即null,因为此时没有执行ajax 函数。

要解决此问题,您需要从将 json 作为参数传递的 ajax-success 方法中调用另一个函数,而不是将其分配给变量

于 2012-11-14T11:26:40.893 回答