-3

I want to load locally stored data using plain javascript (no jquery for example) and then use it to display it in a table in html. My project structure looks like this:

root
- js
-- main.js
- res
-- data.csv
- index.html

I tried using a XMLHttpRequest, but somehow I get status 0 when trying to load the file and when printing the response text it prints out nothing at all.

The following method is called using window.onload:

var url = "file://../res/data.csv/";
varxmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
  if (this.readyState === 4 && this.status === 200) {
    console.log(this.responseText);
  }
};

xmlhttp.open("GET", url, true);
xmlhttp.send();
Dogyman
  • 57
  • 8
  • could be something related to this https://stackoverflow.com/questions/38344612/ajax-request-to-local-file-system-not-working-in-chrome – albert Oct 30 '19 at 23:48
  • Does this answer your question? [AJAX request to local file system not working in Chrome?](https://stackoverflow.com/questions/38344612/ajax-request-to-local-file-system-not-working-in-chrome) – Luca Kiebel Oct 30 '19 at 23:49
  • @LucaKiebel No, because I cant use jQuery – Dogyman Oct 30 '19 at 23:59
  • This has nothing to do with using jQuery or not using it. It doesn't work without some tweaks I wouldn't recommend. – Luca Kiebel Oct 31 '19 at 00:11
  • please check this url [no-jquery](http://youmightnotneedjquery.com/) – asmanp2012 Oct 31 '19 at 00:26

1 Answers1

0

I don't think you can access the file system through most browsers.

Even if it worked locally, it wouldn't work as soon as you put your page up on a server. This is because the client will be asking for a file that is local to itself, not the server.

Chrome may have something for you though: https://www.html5rocks.com/en/tutorials/file/filesystem/

Wolf
  • 441
  • 4
  • 10