我正在使用d3中的散点图。每个点代表一张纸。点击一个点我想与数据库建立连接,并检索由同一作者和所选纸张创作的论文信息,并在图表上显示这些论文。我在我的浏览器上运行了与查询的连接,并手动输入了一个我知道在我的数据库中创作过多篇论文的作者。这将检索正确的信息。但是当我尝试在d3中使用相同的连接时,我可以直接在控制台中看到authorName没有被抓取并且未定义。因此,稍后会在Uncaught TypeError: Cannot read property 'forEach' of undefined
上抛出以下错误。以下是连接的相关代码:
clickHandler(d,i){
d3.json("connection11.php?authorName="+d.AUTHOR, function(error, dataJson) {
console.log(dataJson); //undefined meaning it hasnt grabbed any data..?
dataJson.forEach(function(d) {
d.YEAR = +d.YEAR;
d.counter = +d.counter;
d.type = "common author";
allData.push(d);
console.log(d.type);
console.log(d);
refreshGraph();
})
})
}
有人可以解释为什么会发生这种情况以及我应该做些什么来解决这个问题?我是d3的新手,非常感谢任何帮助!
编辑: http://localhost/D3/connection11.php?authorName=%27Serge%20Abiteboul%27这是包含查询的脚本返回:
[{"ID":"69","AUTHOR":"Serge Abiteboul","In_order":"1","TITLE":"Foundations of Databases.\n","YEAR":"1995","Publication_Venue":"\n","In_library":"1","ID_from":"69","ID_to":"4","counter":"63"},{"ID":"2154","AUTHOR":"Serge Abiteboul","In_order":"1","TITLE":"Object Identity as a Query Language Primitive.\n","YEAR":"1992","Publication_Venue":"Building an Object-Oriented Database System, The Story of O2\n", "In_library":"0","ID_from":"2154","ID_to":"139","counter":"4"}]
据我所知,哪个是可解析的JSON。这是找到的错误:
SyntaxError: Unexpected token Y
at Object.parse (native)
at Object.ia (http://d3js.org/d3.v3.min.js:3:3975)
at XMLHttpRequest.u (http://d3js.org/d3.v3.min.js:1:10081) undefined