Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将外部文件添加到d3_Javascript_Php_D3.js - Fatal编程技术网

Javascript 将外部文件添加到d3

Javascript 将外部文件添加到d3,javascript,php,d3.js,Javascript,Php,D3.js,我尝试在html中为d3添加php输出。出于测试目的,我尝试使用简单代码 index.html <!DOCTYPE html> <html> <head> <title>including php in javascript</title> <? include ('index.php'); ?> </head> <body> <script>

我尝试在html中为d3添加php输出。出于测试目的,我尝试使用简单代码

index.html

     <!DOCTYPE html>
  <html>

 <head>
  <title>including php in javascript</title>
  <?
 include ('index.php');
  ?>
   </head>

  <body>
 <script>
  var myJSON = <?php echo json_encode($json); ?>;
   document.write(myJSON);
 </script>
</body>
  </html>
哪里错了

index.php输出

    [
   {
  "id":"1",
  "name":"parent",
  "parent":"0",
  "children":[
     {
        "id":"2",
        "name":"c1",
        "parent":"1",
        "children":[
           {
              "id":"3",
              "name":"c11",
              "parent":"2",
              "children":[

              ]
           },
           {
              "id":"4",
              "name":"c12",
              "parent":"2",
              "children":[

              ]
           }
        ]
     },
     {
        "id":"5",
        "name":"c2",
        "parent":"1",
        "children":[
           {
              "id":"6",
              "name":"c21",
              "parent":"5",
              "children":[

              ]
           },
           {
              "id":"7",
              "name":"c22",
              "parent":"5",
              "children":[

              ]
           }
        ]
     },
     {
        "id":"8",
        "name":"c3",
        "parent":"1",
        "children":[

        ]
     }
  ]
 }
]
尝试添加

header('Content-Type: application/json');
以前

$json= json_encode($nestedArray);
echo $json;

要指定返回json格式的数据

您不能在.html文件中添加/包含任何php文件,那么我应该如何在D3的javascript变量中添加php结果似乎您没有
echo
(显示)您的json,即您没有在ajax请求中显示有效的json数据是的,我有,不要用回声。让我来try@programmer尝试暂时删除
'json'
,然后查看您的ajax网络响应(f12)如果有错误,只需屏幕截图我想查看它谢谢您的回答..我尝试过..仍然是空白页..控制台中没有错误
header('Content-Type: application/json');
$json= json_encode($nestedArray);
echo $json;