1

In d3 version 3, I had this code, which worked great:

d3.html("http://google.com",function(frag){
    console.log(frag);
});

That code doesn't work in version 4. So looking at the API and change log, I thought perhaps this:

d3.html("http://www.google.com").get(function(error,frag){ console.log(frag); });

But this doesn't work either. Does anyone have a working example of d3.v4 for d3.html?

Thanks

emma
  • 784
  • 2
  • 9
  • 23

1 Answers1

2

It's not a problem with d3. It's probably because the page you're requesting doesn't allow for CORS. Whether it's google.com or your server it should include Access-Control-Allow-Origin header to the response if you want to get it via an async call (XMLHttpRequest, aka AJAX) on a page located at different origin than the server.

As a proof see this codepen: http://codepen.io/LukaszWiktor/pen/OXQwRk?editors=0011

In case of missing CORS headers Chrome Dev Console shows following error:

enter image description here

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • Thanks for your answer, but I can't see anything logged to the console on codepen and changing the google link to your link doesn't work in my text editor. You are definitely using d3 version 4? – emma Jul 19 '16 at 11:33
  • Yes, I'm definitely using d3 v4. Please, see my codepen again. Now the codepen console should show: "enable cross-origin resource sharing". Before the log was skipped because it was too large. – Lukasz Wiktor Jul 19 '16 at 12:01
  • Thank you. I can see you're right. It's working in codepen and firefox but not in my text editor or in my fiddle on chrome. https://jsfiddle.net/rk68L92L/2/ I don't understand why it worked so readily in v3 but is now context dependent. Thanks though. – emma Jul 19 '16 at 12:16
  • Are you sure it worked in v3 with the same page you are trying to load with v4? And about your fiddle - it doesn't work because of "Mixed Content" error - jsfiddle.net is on https while enable-cors.org is on http. – Lukasz Wiktor Jul 19 '16 at 12:39
  • Yes, definitely, it worked in v3 for google.com - the original example i gave - and what's more it worked in my Coda 2 preview screen, which the new version does not. I noticed the https / http conflict message, but (again) I'm surprised that this should matter. I had not previously tested this aspect in v3 tho. Anyway thanks for your help. – emma Jul 19 '16 at 14:02