gpt4 book ai didi

javascript - AJAX 响应 200,但 XAMPP 本地主机上运行的 PHP 脚本没有 JSON 响应数据

转载 作者:行者123 更新时间:2023-12-03 09:28:17 25 4
gpt4 key购买 nike

HTML 代码调用 localhost function getNodes ,该函数以 JSON lint 验证的 JSON 进行响应;但是,当使用 AJAX 从 javascript 文件调用同一函数时,会收到 200 响应代码,但不会收到响应数据。我花了 2.5 天的时间搜索、研究和试验各种 AJAX 内容和数据类型以及 php 文件头、选项。我尝试过相对 URL 和绝对 URL。我已确认以下 Mime 类型XAMPP MIME 文件中有效

  • 应用程序/json 应用程序/javascript JavaScript

仍然是200响应,但是通过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>

这是生成有效 JSONPHP 脚本。

<?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": {}
}
}
}

提前感谢您的帮助。

最佳答案

试试这个

<?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");

关于javascript - AJAX 响应 200,但 XAMPP 本地主机上运行的 PHP 脚本没有 JSON 响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com