2013-05-12 65 views
2

我創建了一個PHP文件來查詢JSON輸出。從PHP文件輸出JSON特定濾鏡「testPHP.php?數= 123」是使用d3.json從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是造成這個問題?

+0

您的JSON無效。你如何生產這個JSON?數組中的最後一個對象缺少一個引號。 – Brad 2013-05-12 21:33:46

+0

錯字錯誤。現在改變了。 – Zero 2013-05-12 21:50:01

回答

2

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 
-1

你需要寫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"}]

此時鏈接將持有該信息。你可能想要考慮一個數組。

+0

我需要使用php文件的json輸出! – Zero 2013-05-12 21:46:11