2015-04-07 53 views
0

我有下面的代碼在過去30天來顯示文件的下載:Morris.js無法顯示Ajax的返回的數據

做一個簡單的查詢到的所有數據來源於「AJAX/downloads.php」我的數據庫。

var pastDays = 30; //this value shows how many days back to show statistics 
    $.ajax({ 
     type: "POST", 
     cache: false, 
     url: "ajax/downloads.php", 
     dataType: "json", 
     data: "days="+pastDays, 
     success : function(res) { 
      var obj = []; 
      for(var x = 0; x <= pastDays; x++){ 
       var currObj = res[x]; 
       var objCombo = currObj.split("|"); 
       var objItem = "{date: '"+objCombo[0]+"', downloads: '"+objCombo[1]+"'}"; 
       objItem = JSON.stringify(eval("(" + objItem + ")")); 
       obj.push(objItem); 
      } 
      console.log(obj); 
} 

這是Ajax響應(RES),我已經得到:

console.log(res); 
["07Apr|1", "06Apr|3", "05Apr|12", "04Apr|11", "03Apr|0", "02Apr|0", "01Apr|6", "31Mar|0", "30Mar|7", "29Mar|16", "28Mar|5", "27Mar|5", "26Mar|12", "25Mar|9", "24Mar|4", "23Mar|10", "22Mar|16", "21Mar|2", "20Mar|19", "19Mar|22", "18Mar|10", "17Mar|11", "16Mar|10", "15Mar|19", "14Mar|0", "13Mar|4", "12Mar|14", "11Mar|5", "10Mar|26", "09Mar|7", "08Mar|5"] 

控制檯輸出推後,我得到的是:

console.log(obj); 
["{"date":"07Apr","downloads":"1"}", "{"date":"06Apr","downloads":"3"}", "{"date":"05Apr","downloads":"12"}", "{"date":"04Apr","downloads":"11"}", "{"date":"03Apr","downloads":"0"}", "{"date":"02Apr","downloads":"0"}", "{"date":"01Apr","downloads":"6"}", "{"date":"31Mar","downloads":"0"}", "{"date":"30Mar","downloads":"7"}", "{"date":"29Mar","downloads":"16"}", "{"date":"28Mar","downloads":"5"}", "{"date":"27Mar","downloads":"5"}", "{"date":"26Mar","downloads":"12"}", "{"date":"25Mar","downloads":"9"}", "{"date":"24Mar","downloads":"4"}", "{"date":"23Mar","downloads":"10"}", "{"date":"22Mar","downloads":"16"}", "{"date":"21Mar","downloads":"2"}", "{"date":"20Mar","downloads":"19"}", "{"date":"19Mar","downloads":"22"}", "{"date":"18Mar","downloads":"10"}", "{"date":"17Mar","downloads":"11"}", "{"date":"16Mar","downloads":"10"}", "{"date":"15Mar","downloads":"19"}", "{"date":"14Mar","downloads":"0"}", "{"date":"13Mar","downloads":"4"}", "{"date":"12Mar","downloads":"14"}", "{"date":"11Mar","downloads":"5"}", "{"date":"10Mar","downloads":"26"}", "{"date":"09Mar","downloads":"7"}", "{"date":"08Mar","downloads":"5"}"] 

然而,當我將其提供給莫里斯.bar我得到「未定義」的值。

var obj = [ {"date":"07Apr","downloads":"1"}, {"date":"06Apr","downloads":"3"}, {"date":"05Apr","downloads":"12"}, {"date":"04Apr","downloads":"11"}, {"date":"03Apr","downloads":"0"}, {"date":"02Apr","downloads":"0"}, {"date":"01Apr","downloads":"6"}, {"date":"31Mar","downloads":"0"}, {"date":"30Mar","downloads":"7"}, {"date":"29Mar","downloads":"16"}, {"date":"28Mar","downloads":"5"}, {"date":"27Mar","downloads":"5"}, {"date":"26Mar","downloads":"12"}, {"date":"25Mar","downloads":"9"}, {"date":"24Mar","downloads":"4"}, {"date":"23Mar","downloads":"10"}, {"date":"22Mar","downloads":"16"}, {"date":"21Mar","downloads":"2"}, {"date":"20Mar","downloads":"19"}, {"date":"19Mar","downloads":"22"}, {"date":"18Mar","downloads":"10"}, {"date":"17Mar","downloads":"11"}, {"date":"16Mar","downloads":"10"}, {"date":"15Mar","downloads":"19"}, {"date":"14Mar","downloads":"0"}, {"date":"13Mar","downloads":"4"}, {"date":"12Mar","downloads":"14"}, {"date":"11Mar","downloads":"5"}, {"date":"10Mar","downloads":"26"}, {"date":"09Mar","downloads":"7"}, {"date":"08Mar","downloads":"5"} ]; 

注意兩個數組之間的區別:從阿賈克斯

我得到的,如果我的手在我的代碼指定「VAR OBJ」,並省略了ajax

Morris.js工作正常,雖然:

["{"date":"07Apr","downloads":"1"}", "{"date":"06Apr","downloads":"3"}", ...] 

雖然這工作的一個是這樣的:

[ {"date":"07Apr","downloads":"1"}, {"date":"06Apr","downloads":"3"}, ...] 

最後,我這是怎麼開始莫里斯:

 Morris.Bar({ 
      element: 'bar-requests', 
      data: obj, 
      xkey: ['date'], 
      ykeys: ['downloads'], 
      labels: ['downloads'] 
     }); 

任何想法,我如何能返回的數據養活我的AJAX莫里斯?

+0

當你調用'''Morris.Bar''',前Ajax調用有回報是它正在創建?這將是我的猜測 –

+0

@ChrisC即使這種情況是真實的 - 我不能說,因爲我第一次使用morris.js,必須有一種方法來填充動態生成的數據... –

回答

0

也許我並沒有正確地表達這個問題,因爲我沒有得到任何答案,我想簡化它並將其作爲一個新問題發佈(希望我沒有違反任何網站rulez)...無論如何,問題解決了!!!

這裏是爲我工作的解決方案: answer by Touhid Alam