I’m trying to use fetch to open a .csv file, but i get this message:
Uncaught TypeError TypeError: Failed to fetch
I am using Visual Studio Code and Chrome there.
my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Read CSV File</title>
</head>
<body>
<div id="output"></div>
<script>
// Function to fetch the CSV file
function fetchCSV(url) {
return fetch(url)
.then(response => response.text())
.then(csv => processData(csv));
}
// Function to process the CSV data
function processData(csv) {
var rows = csv.split('n');
var output = document.getElementById('output');
output.innerHTML = '';
for (var i = 0; i < rows.length; i++) {
var columns = rows[i].split(',');
if (columns.length >= 3) {
output.innerHTML += columns[2] + '<br>';
}
}
}
// Preload and process the CSV file
fetchCSV('Book1.csv'); // Replace 'example.csv' with the your CSV file
</script>
</body>
</html>
Book1.csv is in the same folder of the file that holds the code.
Kind regards
I tried running in different browsers.
A different .csv file because I thought it was too big, the new file is smaller.