19

I'm just learning d3, and I'm attempting to import data from a CSV file, but I keep getting the error "XMLHttpRequest cannot load file:///Users/Laura/Desktop/SampleECG.csv. Cross origin requests are only supported for HTTP. ". I've searched for how to fix this error and have ran it on a local web server, but I haven't found a solution that works for d3.v2.js. Here's a sample of the code:

var Time = []
    ECG1 = []

d3.csv("/Desktop/d3Project/Sample.csv", function(data) 
      {
      Time = data.map(function(d) {return [+d["Time"]];});
      ECG1 = data.map(function(d) {return [+d["ECG1"]];});
      console.log(Time)
      console.log(ECG1)
      });

Any help will be much appreciated.

5
  • 1
    Are you running a local webserver? See e.g. this tutorial. Commented Jan 8, 2014 at 21:40
  • What issues have you encountered with the local web server? Ajax requests won't work with the file: protocol, so a web server (whether it's local or not) is required. Ideally would would server both the script and the csv file from the same server. Commented Jan 10, 2014 at 18:43
  • I have faced the same problem. check do you gave correct file path? and to open file:/// , You should perform the request using local server. Try to open with firefox first. since it handle the request exception. And please provide more information as much as possible (i.e) your console logs and some more information. Commented Sep 25, 2014 at 5:49
  • If your goal is to run a local web server with minimal hassle, consider a browser extension such as Web Server for Chrome. Commented Jun 22, 2018 at 17:03
  • I posted some solutions over here Commented Sep 13, 2020 at 19:14

4 Answers 4

34

This confused me too (I am also a d3 beginner).

So, for some reason, web browsers are not happy about you loading local data, probably for security reasons or something. Anyways, to get around this, you have to run a local web server. This is easy.

In your terminal, after cd-ing to your website's document root (thanks @daixtr), type:

python -m SimpleHTTPServer 8888 &

Okay, now as long as that terminal window is open and running, your local 8888 web server will be running.

So in my case, originally the web page I was working on was called

file://localhost/Users/hills/Desktop/website/visualizing-us-bls-data-inflation-and-prices.html

When I opened it in chrome. To open up my page on my local web server, I just typed (into the chrome search bar):

http://localhost:8888/Desktop/website/visualizing-us-bls-data-inflation-and-prices.html

Now, reading in CSVs should work. Weird, I know.

Sign up to request clarification or add additional context in comments.

4 Comments

I tried this and end up getting this error in console: XMLHttpRequest cannot load localhost:8888/test.csv. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. Have you ran into this as well?
Had the same issue as your comment, just request the html page the same way as the .csv so that they are of the same origin. So instead of opening the .html from the file in windows explorer, request it in the browser: http://localhost:8888/Desktop/website/mypage.html
SimpleHTTPServer has been renamed to http.server in python 3
I'd also add that you can access Terminal and drag & drop the file in, hit enter, get an error, then copy in the python code.
6

To those using built-in python webserver and who are still experiencing issues, do REMEMBER and make sure that you run the "python -m SimpleHTTPServer 8888" invocation at the correct path of which you consider to be your DocumentRoot. That is, you cannot just run 'python -m SimpleHTTPServer 8888' anywhere. You have to actually 'cd /to/correct/path/' containing your index.html or data.tsv and then from there run 'python -m SimpleHTTPServer 8888'.

1 Comment

That's a great point. I added it to the main answer so people see.
6

Also, just learning D3 for school work. I was trying to run this simple D3 example: https://gist.github.com/d3noob/b3ff6ae1c120eea654b5

I had the same problem as OP re: loading data using Chrome browser. I bet the great solution Hillary Sanders posted above was re: Python 2.X.

My answer is re: Python 3.X [OS: Ubuntu 16x]:

Open a terminal window within the root directory of your project, then run:

python3 -m http.server

It will serve HTTP on port 8000 by default unless it is already taken, in that case to open another port, e.g. 7800, run:

python3 -m http.server 7800

Then, on your Chrome browser address bar type:

localhost:8000

The above worked for me because I only had an index.html page in my root folder. In case, you have a HTML page with a different name, type the whole path to that local HTML page and it should work also. And, you should be able to see the graph created from the data set in my link (that must be in a folder like data/data.csv). I hope this helps. :-)

Comments

1

Use Firefox, idk what Chrome tries to accomplish

3 Comments

Want to know the reason why chrome wasn't able to do this
I faced the same CORS issue on Firefox as well.
Incorrect. CORS is not Chrome-specific.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.