When I am running the following code:
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:
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
If you are studying/prototype (by the looks of it) I would go with the second approach.
Very late to the party, but for anyone still having issues, it is really easy to set up an http-server instance with NPM from the directory containing all of your files you want to server.
You would just install http-server
globally npm i http-server -g
, then run the http-server
from your root directory.
Once the server is running, go to your browser and input the relative path prefixed with localhost:[port][/path/to/file]
. Port will be printed out from the command line when you run the http-server for the first time within your repo/directory, and the relative path starts with the directory you ran the http-server. This will properly serve files to the end user, without just copy and pasting a link to the literal local.