1

When running the following code in Chrome, d3.json callback is not triggered until pubs.json is loaded in another browser tab/window. It works when the d3.json is pasted into the Chrome browser console, it works if running in Firefox...

I'm using Python's http.server on localhost.

Why does it act this way in Chrome?

dir structure:

proj/
  index.html
  pubs.json

code:

<html>
  ...
  <body>
    ...
    <script>
        $(document).ready(function(){
            d3.json("/pubs.json").then(function(data) {
                console.log(data);
            });
        });
    </script>
  </body>
</html>
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
conner.xyz
  • 6,273
  • 8
  • 39
  • 65
  • What do you mean by "until `pubs.json` is loaded in another browser tab/window" ? Do you have a link to reproduce your issue ? – Pierre Capo Jul 30 '18 at 21:35
  • Script tag should be inside body tag. Is that only typo while rewriting to this post? – bigless Jul 30 '18 at 21:45
  • I know this might be a silly question, but have you checked if you are running a proper server to test that code? Javascript in general has several limitations when not working with proper permissions, and while firefox and safari lets you circumvent this somewhat, chrome doesn't. – Manuel Alejandro Caetano Jul 30 '18 at 22:49
  • @ManuelAlejandroCaetano probably not "proper". I'm using Python's `http.server`. Thought I've never had this problem before. – conner.xyz Jul 31 '18 at 01:30
  • @PierreCapo I mean that – the best of my knowledge – the promise is pending on loading the page. Then if, in another browser tab/window, I load `/pubs.json` the promise fulfills in the original tab/window. – conner.xyz Jul 31 '18 at 01:34
  • If it's a CORS issue, maybe enable CORS on Python server? https://stackoverflow.com/questions/21956683/enable-access-control-on-simple-http-server – NoobTW Jul 31 '18 at 01:45

1 Answers1

-1

Chrome is pedantic regarding CORS (Same Origin)

Try the following:

d3.json(chartURL, {credentials: 'same-origin'}).then(function(data) {
    ....
});

If the file is loaded by another tab it can be found in the cache and maybe then the requirements are not that strict any more.

rioV8
  • 24,506
  • 3
  • 32
  • 49