我创建了一个PHP文件来查询JSON输出。对于特定的过滤器"testPHP.php?number=123“,PHP文件的JSON输出是
[{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW","target":"EA","type":"lie"}]我在html文件中尝试过,以读取JSON输出到链接变量
var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
if (error) return console.warn(error);
links = json;
});但是,看起来数据并不是存储在链接中。如何在var links中存储数据
基本上,我希望将此https://gist.github.com/mbostock/1153292中的var links替换为来自PHP的JSON输出中的JSON。
编辑:还是http://localhost:8080导致了问题?
发布于 2013-05-13 06:30:49
been调用是异步的;回调之后包含的代码是在填充"links“变量之前执行的。
var links; // a global
d3.json("testPHP.php?number=123", function(error, json) {
if (error) return console.warn(error);
links = json;
console.log(links); // your links are populated here
// include the rest of your app here
});
// links is still undefined here because the success callback hasn't been run yet发布于 2013-05-13 05:34:27
您需要编写links = [{"source":"AB","target":"AC","type":"true"},{"source":"UB","target":"EP","type":"true"},{"source":"US","target":"UR","type":"lie"},{"source":"BS","target":"QW","type":"lie"},{"source":"UW,"target":"EA","type":"lie"}]
在这一点上,链接将保存该信息。但是,您可能希望考虑一个数组。
https://stackoverflow.com/questions/16512218
复制相似问题