我試圖實現一個函數,它會加載一個服務器存儲的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稍晚一點。有一個簡單的解決方案嗎?
感謝,
你爲什麼不這樣做處理服務器端,並使用JSON來傳輸呢? – Femaref 2011-06-13 10:20:53