getting json data from localhost to use in D3

穿精又带淫゛_ 提交于 2020-01-05 08:18:11

问题


I'm creating a D3 bubble chart, using json data. I'm using sinatra for my app, and my json data is available at localhost:4567/data.json

I tried using

var myData = [];
$.get('data.json', function(data) {
  myData = data;
  console.log(myData);
.......

and I get the correct values in the javascript console, but the bubble chart does not render. (The rest of the code works if I copy and paste the data from 'data.json' and set it to a var, but it does not work if I use the $get method).

Do you have any ideas on how I could access this json data from localhost:4567?

Much appreciated,

Tim


回答1:


I think what is probably going on is that jquery isn't automatically parsing the data as a JSON object due to missing MIME headers in the response from your server. Try using getJSON instead.




回答2:


you can simply use d3.json('data.json', function(data) { myData = data; console.log(myData); .......

to read the json file



来源:https://stackoverflow.com/questions/18005484/getting-json-data-from-localhost-to-use-in-d3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!