2014-12-07 31 views
-2

我有一個AJAX功能返回的日期,我不能把這個日期...日期從數據庫返回,並把它放在一個數組

var dates = []; 

    $.ajax({ 
     type : "POST", 
     url : "agenda.php?action=getData", 
     success: function(data){ 
      dates.push(data); 
     } 
    }); 

    console.log(dates); // here prints [] .... =/ .... and i made other test i put var dates = [] outside of $(document).ready... and nothing 
+0

'agenda.php?action = getData'返回什麼? – 2014-12-07 20:42:24

+0

返回像2014/12/20的簡單日期....如果我可以使它工作...我將返回一個json – 2014-12-07 20:47:29

+0

發佈PHP代碼 – 2014-12-07 20:51:09

回答

1

因爲Ajax調用是異步的,執行立即繼續。因此,當執行到console.log時日期仍然是[]。

您將需要在傳遞給$ .ajax的成功回調函數中進行處理。

相關問題