我正在使用jquery.csv-0.71.min.js
lib加載csv文件並將其轉換爲數組。然而,我加載網頁時:ReferenceError:CSV未定義
<script src="assets/lib/jquery/dist/jquery.min.js"></script>
<script src="assets/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<!-- Jquery-csv -->
<script src="assets/js/jquery.csv-0.71.min.js"></script>
<script>
(function() {
var data;
var url = 'data/data.csv';
function makeRequest(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.readyState === 4 && xhr.status === 200) {/*run code*/}
};
xhr.send(null);
}
})();
data = CSV.toArrays(xhr.responseText);
console.log(data);
</script>
我在控制檯中:
ReferenceError: CSV is not defined at 'data = CSV.toArrays(xhr.responseText);'
任何建議我做錯了嗎?
我很感謝你的回覆!
UPDATE
我把我的代碼插入到文檔閱讀功能可按,但是,我仍然得到上述同樣的錯誤。
$(document).ready(function() {
(function() {
var data;
var url = 'data/data.csv';
function makeRequest(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
if (xhr.readyState === 4 && xhr.status === 200) {/*run code*/
}
};
xhr.send(null);
}
})();
data = CSV.toArrays(xhr.responseText);
console.log(data);
});
閱讀:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – epascarello 2014-11-14 15:19:55
我過去曾經使用過這個庫,但是對於處理簡單的CSV文件,只需要自己解析它就簡單多了。順便說一句,在你的例子中,CSV.toArrays()應該在xhr.onload中,它會在讀取XML之前執行。 – nepeo 2014-11-14 15:50:46