我创建了一个 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 输出。
编辑:
还是 http://localhost:8080
导致了问题?
最佳答案
d3.json 调用是异步的;在填充“链接”变量之前执行回调后包含的代码。
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
关于php - 使用 d3.json 从 PHP 输出 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512218/