0

I'm trying to get externally-situated csv data to load up in a script, but this fails I think due to browser same origin policy. I've spotted some relevant looking discussion on working around this using cross-document messaging, but to be honest I don't have a clue how to implement this. Grateful for advice on this or another workaround. The script below should print each data line to the browser console but fails.

<html>
<head>
    <meta charset='utf-8'>
    <script type="text/javascript" src="scripts/d3.v2.js"></script>
    <title>CSV reader</title>
</head>
<body>
    <script type="text/javascript"> 
        d3.csv("http://www.quake.utah.edu/EQCENTER/LISTINGS/OTHER/Yell_Q32012TT.csv", 
        function(parseCoords) {
          parseCoords.forEach(function(d) {
            console.log(d);
          });
        });
    </script>
</body>
</html>
4
  • The link to the page is a 404. Commented Jan 2, 2013 at 1:50
  • The Lars Kotthoff of mayamap.org fame and MRP? Link works fine for me, also with http instead of https. d3.geotheory.co.uk/csv2.html links to another existing csv example but also fails. Commented Jan 2, 2013 at 10:47
  • Hmm, the first link is still not working for me, but the second is. Anyway, one way of getting around the same-origin requirements is JSONP, although that would require you to convert the data to JSON and is a bit hacky in general. The best solution is to put the data on the same server as the script. Commented Jan 2, 2013 at 17:56
  • Yes I'll probably have to do this in some cases, but sadly not a solution for csv-API resources. Commented Jan 3, 2013 at 0:06

3 Answers 3

1

If you are doing something like writing HTML and Javascript in a code editor on your personal computer, and testing the output in your browser, you will probably get error messages about Cross Origin Requests. Your browser will render HTML and run Javascript, jQuery, angularJs in your browser without needing a server set up. But many web browsers are programed to watch for cross site attacks, and will block requests. You don't want just anyone being able to read your hard drive from your web browser. You can create a fully functioning web page using Notepad++ that will run Javascript, and frameworks like jQuery and angularJs; and test everything just by using the Notepad++ menu item, RUN, LAUNCH IN FIREFOX. That's a nice, easy way to start creating a web page, but when you start creating anything more than layout, css and simple page navigation, you need a local server set up on your machine.

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

Comments

0

I have seen similar problems linking to a file in the drop box public folders area. (specifically https://dl.dropbox.com/u/101577503/D3-Tips-and-Tricks-Latest.pdf). The first time you click on the link from a web page, the downloading of the pdf fails, then refresh and it works. I don't know why it occurs, but I get the same result when I test your 'http://dl.dropbox.com/u/46043231/data/xy.csv' in Chrome. Is it also possible that even if it works first time that you will be stymied by cross domain limitations?

6 Comments

I've never had that - it may be something with your browser settings. Nice paper though!
Cheers. I've seen it in my browser and had reports from others that have seen the same. I have a feeling you're right and it is probably browser based, so I'll transition hosting the files to GitHub. Is the cross domain issue going to affect you?
I think it is a cross-domain issue as you say. I'm wondering if there is a general javascript workaround to it as I can't believe its not possible to import a simple csv from the web!
I agree, and while I haven't investigated how to do it, I know that Chris Viau has posted some detail on doing it here; groups.google.com/group/d3-js/t/3e581f077d160f14 (About the 7th entry down) I had a quick play with this myself and it didn't work for me, so I'm starting to wonder about the browser I'm using... Hmm....
Hmm... Perhaps not, I just installed Firefox and have the same result. (I use Chrome normally). Check with Chris on the link above. If you can find a work around, drop me a line and I'll include it in the D3 Tips and Tricks :-)
|
0

Question abandoned because it seems unanswerable. Will update if I find a solution at a later date.

Comments

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.