2011-06-13 22 views
2

我試圖實現一個函數,它會加載一個服務器存儲的CSV文件,並將其解析爲一個數組。我有一些問題,並會很感激的幫助。CSV到數組 - jquery和ajax問題

這裏的功能看起來像此刻:

function getSeries(filename){ 
    //create an array where objects will be stored. 
    var seriesInFile=[]; 
    $.get('/public/csv/'+filename+'.csv', 
     function(data) { 
      // Split the lines 
      var lines = data.split('\n'); 
      $.each(lines, function(lineNo, line) { 
       var items = line.split(','); 
       //elements from each line are taken and stored 
       series = { 
          //some other attributes here, I took them off for this example 
          data: [] 
         } 
       $.each(items, function(itemNo, item) { 
         series.data.push(parseFloat(item)); 
       }); 
       seriesInFile.push(series); 
      }); 
     console.log(seriesInFile); //it works here! 
     }); 
    return seriesInFile; 
    }; 
    console.log(getSeries('hr')); 
    console.log(getSeries('hr')); 

那麼,問題是,即使我成功地從CSV文件中的$。每個函數中獲取信息,並將其存儲「seriesInFile」內,只要我離開它不再工作 - 整個函數返回一個空數組。 我想這可能與get()函數部署比原始getSeries稍晚一點。有一個簡單的解決方案嗎?

感謝,

+0

你爲什麼不這樣做處理服務器端,並使用JSON來傳輸呢? – Femaref 2011-06-13 10:20:53

回答

1

我認爲你必須從$不用彷徨

function getSeries(filename){ 
    //create an array where objects will be stored. 
    var seriesInFile=[]; 
    $.get('/public/csv/'+filename+'.csv', 
     function(data) { 
      // Split the lines 
      var lines = data.split('\n'); 
      $.each(lines, function(lineNo, line) { 
       var items = line.split(','); 
       //elements from each line are taken and stored 
       series = { 
          //some other attributes here, I took them off for this example 
          data: [] 
         } 
       $.each(items, function(itemNo, item) { 
         series.data.push(parseFloat(item)); 
       }); 
       seriesInFile.push(series); 
      }); 
     console.log(seriesInFile); //it works here! 
     // do what you need to do with seriesInfile here 
     //like calling a function to display it 

     }); 
     //here seriesInFile is an empty array; 
    }; 

回調函數返回seriesInFile因爲$不用彷徨向服務器異步調用,其他所有的代碼被執行:在你的例子中,你將serialInFile作爲一個空數組返回,因爲你的函數不會等待AJAX​​調用完成,只是在聲明它時返回serialInFile。 如果你想返回它,你必須在AJAX調用成功執行的$ .GET的回調函數中這樣做。

編輯 - 正如從回調返回的評論建議可能不起作用(從未嘗試過)。通常你應該做任何你需要直接從回調函數中完成的操作(這就是我通常所做的)。如果您需要特別幫助,我可以幫助

+0

你的分析是正確的:GET請求是異步的,並且在getSeries函數返回後很久就完成了。我不認爲你的代碼是有用的,但。從回調中返回數組不會有任何區別。 – Codo 2011-06-13 10:55:43

1

正如Nicola Peluchetti正確指出的,GET請求是異步的。這意味着它會在您撥打$.get時開始,但會在稍後完成。函數getSeries立即返回。此時,數組仍然是空的,因爲GET請求尚未完成。

無論您想對解析的CSV數據執行什麼操作,您都必須從GET請求的回調中啓動它,而不是從調用getSeries(最好稱爲startSeriesRetrieval)的函數啓動它。

因此,它可能看起來像這樣(displaySeries稱爲進一步與檢索到的數據進行工作):

function startSeriesRetrieval(filename) { 
    $.get('/public/csv/'+filename+'.csv', 
    function(data) { 
     // Split the lines 
     var lines = data.split('\n'); 
     var seriesInFile=[]; 
     $.each(lines, function(lineNo, line) { 
     var items = line.split(','); 
     //elements from each line are taken and stored 
     series = { 
      //some other attributes here, I took them off for this example 
      data: [] 
     } 
     $.each(items, function(itemNo, item) { 
      series.data.push(parseFloat(item)); 
     }); 
     seriesInFile.push(series); 
     }); 
     displaySeries(seriesInFile); 
    } 
); 
}; 
+0

非常感謝你們兩位,這對我們幫助很大 – Klon 2011-06-14 13:05:58