D3-通过AJAX调用传递JSON数据 [英] D3 - passing JSON data through AJAX call
问题描述
我正在使用Ajax从服务器调用JSON格式的数据,并且成功将其传递给D3时.
I'm using Ajax to call data which is in JSON format from the server and when its successful want to pass it to D3.
$(document).ready(function() {
$.ajax({
url: "{% url 'charts_data' %}",
method: 'GET',
data : {
airline_category: 1,
year_category: 5
},
success: function(data){
console.log(data) // Correctly logs data to console
d3.json(data, function(dataSet){
console.log(dataSet) // Null with error 404
})
},
error: function(error_data){
console.log("error")
console.log(error_data)
}
})
});
我了解我需要向d3.json提供URL而不是数据.但是我想使用这个称为数据对象的Ajax来构建图表.称为data的Ajax是JSON格式,如何在D3中使用它?
I understand I need to provide URL instead data to d3.json. But I want to use this Ajax called data object to build the chart. Ajax called data is in JSON format, how can I use it with D3?
这是数据的console.log:
Here is console.log of data:
console.log(数据)
console.log(data)
推荐答案
如果您已经拥有ajax
调用中的数据,请不必理会d3.json()
.只需执行success
中的所有操作,否则就可以在d3.json
正文中进行此操作.
Don't bother with d3.json()
if you already have the data from the ajax
call. Just do everything in success
that you would otherwise have done in the body of d3.json
.
这篇关于D3-通过AJAX调用传递JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!