HTML
代码调用 localhost function
getNodes
,它使用JSON lint验证的JSON进行响应;但是,当使用来自javascript文件的AJAX调用相同的函数时,会收到200响应代码,但没有响应数据。我花了2.5天时间搜索,研究和试验各种AJAX内容和数据类型以及php文件头,选项。我已经尝试了相对与绝对的网址。我已确认以下 Mime类型在 XAMPP MIME 文件中有效
仍然是200 response
,但在通过 AJAX 进行调用时没有响应数据。我已经在下方添加了javascript和HTML。
$( document ).ready(function(){
var getNodesUrl = "../getNodes.php";
$.ajax({
type: 'POST',
url: getNodesUrl,
datatype: "json",
contentType: "application/json",
data: { classification: "skills" },
success: function(nodes){
console.log("successful ajax call");
console.log(nodes);
var sys = arbor.ParticleSystem(1000, 400,1);
sys.parameters({gravity:true});
sys.renderer = Renderer("#viewport");
sys.graft(nodes);
},
error: function(){
console.log("ajax call failed");
}
})
});

<html>
<head>
<script language="javascript" type="text/javascript" src="http://localhost/lab/public/scripts/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="http://localhost/lab/public/scripts/arbor.js" ></script>
<script language="javascript" type="text/javascript" src="http://localhost/lab/public/scripts/graphics.js" ></script>
<script language="javascript" type="text/javascript" src="http://localhost/lab/public/scripts/renderer.js" ></script>
</head>
<body>
<?php
require("../getNodes.php");
$_POST['classification'] = "skills";
getNodes();
?>
<canvas id="viewport" width="800" height="600"></canvas>
<script language="javascript" type="text/javascript" src="../scripts/index.js" ></script>
</body>
</html>
&#13;
以下是生成有效 JSON 的 PHP 脚本。
<?php
function getNodes()
{
require_once("../../includes/config.php");
header('Content-Type: json');
$classification = $_POST['classification'];
$nodes = array();
$edges = array();
$i = 0;
$classes = query("SELECT * FROM classifications WHERE label = ?", $classification);
if (sizeof($classes) != 0)
{
$nodes = '{"nodes":{"' . $classification . '":{"color":"blue","shape":"dot","label":"' . $classification . '"}';
$edges = '"edges":{"' . $classification .'":{ ';
$classes = query("SELECT * FROM classifications WHERE label = ?", $classification);
foreach ($classes as $clas)
{
$nodes = $nodes . ',"' . $clas['child'] . '":{"color":"' . $clas['color'] . '","shape":"dot","label":"' . $clas['child'] . '"}';
$edges = $edges . '"' . $clas['child'] . '":{}';
if ($i < (sizeof($classes)-1))
{
$edges = $edges . ",";
}
$i+=1;
}
$nodes = $nodes . "},";
$edges = $edges . "}}}";
$data = $nodes . $edges;
echo $data;
}
else
{
echo "{}";
}
}
?>
JSON输出
{
"nodes": {
"skills": {
"color": "blue",
"shape": "dot",
"label": "skills"
},
"Biology and life sciences": {
"color": "red",
"shape": "dot",
"label": "Biology and life sciences"
},
"Computer and information sciences": {
"color": "red",
"shape": "dot",
"label": "Computer and information sciences"
},
"Earth sciences": {
"color": "red",
"shape": "dot",
"label": "Earth sciences"
},
"Ecology and environmental sciences": {
"color": "red",
"shape": "dot",
"label": "Ecology and environmental sciences"
},
"Engineering and technology": {
"color": "red",
"shape": "dot",
"label": "Engineering and technology"
},
"Medicine and health sciences": {
"color": "red",
"shape": "dot",
"label": "Medicine and health sciences"
},
"People and places": {
"color": "red",
"shape": "dot",
"label": "People and places"
},
"Physical sciences": {
"color": "red",
"shape": "dot",
"label": "Physical sciences"
},
"Research and analysis methods": {
"color": "red",
"shape": "dot",
"label": "Research and analysis methods"
},
"Science policy": {
"color": "red",
"shape": "dot",
"label": "Science policy"
},
"Social sciences": {
"color": "red",
"shape": "dot",
"label": "Social sciences"
}
},
"edges": {
"skills": {
"Biology and life sciences": {},
"Computer and information sciences": {},
"Earth sciences": {},
"Ecology and environmental sciences": {},
"Engineering and technology": {},
"Medicine and health sciences": {},
"People and places": {},
"Physical sciences": {},
"Research and analysis methods": {},
"Science policy": {},
"Social sciences": {}
}
}
}
&#13;
我提前感谢您的帮助。
答案 0 :(得分:1)
试试这个
<?php
function getNodes()
{
require_once("../../includes/config.php");
header('Content-Type: json');
$classification = $_POST['classification'];
$nodes = array();
$edges = array();
$i = 0;
$classes = query("SELECT * FROM classifications WHERE label = ?", $classification);
if (sizeof($classes) != 0)
{
$nodes = '{"nodes":{"' . $classification . '":{"color":"blue","shape":"dot","label":"' . $classification . '"}';
$edges = '"edges":{"' . $classification .'":{ ';
$classes = query("SELECT * FROM classifications WHERE label = ?", $classification);
foreach ($classes as $clas)
{
$nodes = $nodes . ',"' . $clas['child'] . '":{"color":"' . $clas['color'] . '","shape":"dot","label":"' . $clas['child'] . '"}';
$edges = $edges . '"' . $clas['child'] . '":{}';
if ($i < (sizeof($classes)-1))
{
$edges = $edges . ",";
}
$i+=1;
}
$nodes = $nodes . "},";
$edges = $edges . "}}}";
$data = $nodes . $edges;
$content = $data;
}
else
{
$content = "{}";
}
header("Content-Type: application/json");
echo $content;
}
?>
注意PHP文件中的header("Content-Type: application/json");
。
答案 1 :(得分:0)
您必须设置contentType
,您的内容类型应为
contentType: "application/json",
您的datatype: "json",
也应该是
dataType: "json",