Origin null is not allowed by Access-Control-Allow-Origin.
d3.js
Solution
For security reasons, browsers blocks Ajax HTTP requests (XHR) from different hosts (origins).
The `d3.json("...")` function makes an Ajax request to your http://localhost/... and you are probably serving the HTML from a different host.
Are you opening the .html as file in the browser? Thats considered a different host. You have some options there:
- Serve your HTML file from the same web server you are serving the json file
- Convert your .json into a .js adding something like `var mygeodata = {your json here}` to the file and adding `<script type="text/javascript" src="http://localhost/webserver/us-states.js"></script>` in the HTML `<head>` while also removing the `d3.json("...")` part. After that you have a global variable with your data in `mygeodata`
- Configure your web server to allow CORS.
If you are studying/prototype (by the looks of it) I would go with the second approach.
Problem
When I am running the following code: ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>D3 Demo: GeoJSON</title> <script type="text/javascript" src="http://localhost/webserver/d3/d3.js"></script> <style type="text/css"> /* No style rules here yet */ </style> </head> <body> <script type="text/javascript"> //Width and height var w = 500; var h = 300; //Define default path generator var path = d3.geo.path(); //Create SVG element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); //Load in GeoJSON data d3.json("http://localhost/webserver/us-states.json", function(json) { //Bind data and create one path per GeoJSON feature svg.selectAll("path") .data(json.features) .enter() .append("path") .attr("d", path); }); </script> </body> </html> ``` I get the following error: ``` XMLHttpRequest cannot load http://localhost/webserver/us-states.json. Origin null is not allowed by Access-Control-Allow-Origin. ``` What is going wrong here and how do I solve it? I am following the book of Scott Murray and I didn't have problems to access files so far on my webserver until I started with json.