JSON中的意外标记u位于JSON_从php编码为javascript的位置0处
我正在尝试使用d3中基于表单输入的index.php文件 index.htmlJSON中的意外标记u位于JSON_从php编码为javascript的位置0处,javascript,php,jquery,html,d3.js,Javascript,Php,Jquery,Html,D3.js,我正在尝试使用d3中基于表单输入的index.php文件 index.html <!DOCTYPE html> <meta charset="utf-8"> <body> <script src="//d3js.org/d3.v3.min.js"></script> <script>
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var links;
d3.json("index.php", function(error, json) {
links = json;
try{
JSON.parse(json)
} catch(e) {
document.writeln("Caught: " + e.message)
}
});
</script>
(index.php文件返回未定义。)
index.php
<?php
header('Content-Type: text/plain');
if(isset($_GET['input']))
{
$con=mysqli_connect("localhost","root","admin321","php");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name=$_GET['input'];
$sql="SELECT * FROM newtab where value LIKE '%".$name."%'";
if(!$sql){
echo"error".mysqli_error();
}
$r = mysqli_query($con,$sql);
$data = array();
while($row = mysqli_fetch_assoc($r)) {
$data[] = $row;
}
function buildtree($src_arr, $parent_id = 0,
$tree = array())
{
foreach($src_arr as $idx => $row)
{
if($row['parent'] == $parent_id)
{
foreach($row as $k => $v)
$tree[$row['id']][$k] = $v;
unset($src_arr[$idx]);
$tree[$row['id']]['children']
= buildtree($src_arr, $row['id']);
}
}
ksort($tree);
return $tree;
}
function insertIntoNestedArray(&$array, $searchItem){
if($searchItem['parent'] == 0){
array_push($array, $searchItem);
return;
}
if(empty($array)){ return; }
array_walk($array, function(&$item, $key, $searchItem){
if($item['id'] == $searchItem['parent']){
array_push($item['children'], $searchItem);
return;
}
insertIntoNestedArray($item['children'], $searchItem);
}, $searchItem);
}
$nestedArray = array();
foreach($data as $itemData){
$nestedArrayItem['value'] = $itemData['value'];
$nestedArrayItem['id'] = $itemData['id'];
$nestedArrayItem['name'] = $itemData['name'];
$nestedArrayItem['parent'] = $itemData['parent'];
$nestedArrayItem['children'] = array();
insertIntoNestedArray($nestedArray, $nestedArrayItem);
}
header('Content-Type: application/json');
$json= json_encode($nestedArray);
echo $json;
}
?>
没有JSON.parse输出是
更新2:基于Gerardo Furtado删除d3,我在index.html文件中使用了ajax
<script>
$(document).ready(function() {
$.ajax({
url:"index.php",
success:function(result){
alert(result);
}
});
});
</script>
$(文档).ready(函数(){
$.ajax({
url:“index.php”,
成功:功能(结果){
警报(结果);
}
});
});
in返回正确的json,正如我在pastbin中发布的一样。但是我想获取d3的值
更新3
<script>
var jsonData = $.ajax({
type: 'GET',
url: 'index.php',
dataType: 'json',
data: $('#vinform').serialize(),
}).responseText;
var data=jsonData;
document.write(data);
</script>
var jsonData=$.ajax({
键入:“GET”,
url:'index.php',
数据类型:“json”,
数据:$('#vinform')。序列化(),
}).responseText;
var数据=jsonData;
文件。写入(数据);
显示未定义的如果您只是这样做:
d3.json(“index.php”,function(json){console.log(json);})
您在控制台中得到了什么?也许您不需要解析它?控制台是空的。如果您的控制台是空的,那么显然存在返回问题。所以先解决这个问题,让我看看我的更新。如果你这么做:d3.json(“index.php”,function(json){console.log(json);})
你在控制台中得到了什么?也许你不需要解析它?控制台是空的。如果你的控制台是空的,那么显然有返回问题。所以先解决这个问题,请看我的更新。。
try{
alert(json) ;
} catch(e) {
document.writeln("Caught: " + e.message)
}
<script>
$(document).ready(function() {
$.ajax({
url:"index.php",
success:function(result){
alert(result);
}
});
});
</script>
<script>
var jsonData = $.ajax({
type: 'GET',
url: 'index.php',
dataType: 'json',
data: $('#vinform').serialize(),
}).responseText;
var data=jsonData;
document.write(data);
</script>