geotheory
geotheory

Reputation: 23670

Calling externally-located csv data in javascript

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>

Upvotes: 0

Views: 338

Answers (3)

Pranjal Rathod
Pranjal Rathod

Reputation: 11

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.

Upvotes: 1

geotheory
geotheory

Reputation: 23670

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

Upvotes: 0

d3noob
d3noob

Reputation: 2151

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?

Upvotes: 0

Related Questions