来自PHP的JSON输出使用d3.json [英] JSON output from PHP using d3.json

查看:289
本文介绍了来自PHP的JSON输出使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆