I'm trying to parse a local csv file in an Angular/Sinatra project - preferably using D3, but have also tried the HTML5 file API, Papa.parse, and an ajax call. However, I keep getting a 404 error with ajax, d3, and HTML5, and an empty dataset with Papa. The file is saved in the same level as the application.js folder.
Can anyone tell me why?
Here's my code, including many of the various things I've tried:
application.js:
angular.module('Project', ['ngRoute', 'ngResource'])
.config(function($routeProvider) {
$routeProvider
// route for the calculator page
.when('/', {
templateUrl: 'partials/display.html',
controller: 'displayController'
})
// tried giving the csv a route to link to but this didn't work
.when('/data', {
templateUrl: 'partials/dataset.csv',
controller: 'dataController'
})
})
.controller('displayController', function($scope, $http) {
var dataset
// this gives a 404 error
d3.csv('challengedataset.csv', function(data) {
dataset = data;
})
// these give an empty datset
var results = Papa.parse("challenge-dataset.csv", function(data) {
dataset = data
});
Papa.parse('challenge-dataset.csv', {
worker: true,
step: function(row) {
dataset = row
},
complete: function() {
console.log("All done!");
}
});
// these gave me a 404 error
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result;
}
var dataset = reader.readAsText('challenge-dataset.csv');
})
$.ajax({
type : 'GET',
dataType : 'json',
url: 'challenge-dataset.csv',
success : function(data) {
dataset = data
}
});
console.log(dataset)