来自PHP的JSON输出使用d3.json [英] JSON output from PHP using d3.json
问题描述
我创建了一个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: source:UW,target:EA,type:lie}]
$ b b我已经尝试过在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
?
基本上, c $ c> var links 在此 https://gist.github.com/
编辑:
或http:// localhost:8080
解决方案d3.json调用是异步的;
var links;在回调之前执行的代码在links变量填充之前执行。 // a global
d3.json(testPHP.php?number = 123,function(error,json){
if(error)return console.warn(error);
links = json;
console.log(links); //你的链接在这里填充
//包括你的应用程序的其余部分
});
//链接仍未定义,因为成功回调尚未运行
I created a PHP file to query JSON output. JSON output from the PHP file for a particular filter "testPHP.php?number=123" is
[{"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"}]
I have tried this in html file to read the JSON output to links variable
var links; // a global d3.json("testPHP.php?number=123", function(error, json) { if (error) return console.warn(error); links = json; });
But, it seems like data is not stored in links. How do I store the data in
var links
?Basically, I want to replace
var links
in this https://gist.github.com/mbostock/1153292 to that from JSON output from PHP.EDIT: Or
http://localhost:8080
is causing the problem?解决方案The d3.json call is asynchronous; the code included after your callback is executed before the "links" variable has been populated.
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的JSON输出使用d3.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!