0

Can someone explain to me why I keep getting this error message:

XMLHttpRequest cannot load http://mesonet.agron.iastate.edu/iemre/multiday/2013-04-01/2013-04-12/42.54/-96.40/json. Origin ------------ is not allowed by Access-Control-Allow-Origin.

But I can access the data with this URL: http://mesonet.agron.iastate.edu/iemre/multiday/2013-04-01/2013-04-12/42.54/-96.40/json

d3.json("http://mesonet.agron.iastate.edu/iemre/multiday/2013-04-01/2013-04-12/42.54/-96.40/json", function(error, json) {
  if (error) return console.warn(error);
  data = json;

Is this not the appropriate way load an external json file using d3?

bailey
  • 353
  • 6
  • 19
  • what i see you are missing the space after the comma before function – tomtomtom Nov 04 '13 at 22:25
  • I think that just happened when I was pasting the code – bailey Nov 04 '13 at 22:32
  • 1
    This a security measure based on the [Same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy). – aug Nov 04 '13 at 22:46
  • Check this: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – meetamit Nov 04 '13 at 22:47
  • possible duplicate of [XMLHttpRequest cannot load an URL with jQuery](http://stackoverflow.com/questions/3828982/xmlhttprequest-cannot-load-an-url-with-jquery) – aug Nov 04 '13 at 22:47
  • I added &callback=? to the end of the url and am now getting this error: "Uncaught SyntaxError: Unexpected token" but I am able to see the data in the console. – bailey Nov 05 '13 at 18:23

1 Answers1

0

As @aug posted, this violates the Same Origin Policy. You can read more about it at http://en.wikipedia.org/wiki/Same_origin_policy

The only way that you can get around this issue is if you have control of the JSON file and then you can add "padding" to the file. You can read more about that at http://en.wikipedia.org/wiki/JSONP

th3n3wguy
  • 3,649
  • 2
  • 23
  • 30