【问题标题】:AJAX response 200, but no JSON response data from PHP script running on XAMPP localhostAJAX 响应 200,但没有来自在 XAMPP localhost 上运行的 PHP 脚本的 JSON 响应数据
【发布时间】:2015-07-27 19:45:43
【问题描述】:

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

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

仍然是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>

这是生成有效 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": {}
        }
    }
}

提前感谢您的帮助。

【问题讨论】:

  • HTML 代码是您的getNodes.php,如果是,您在哪里回显 json 输出?如果没有,请把getNodes.php的内容也贴上去。
  • 你的控制台说什么,有什么错误吗?
  • 没有调用就无法运行 PHP 函数.. 它必须被调用
  • @jigar,HTML代码和getNodes.php一样粘贴在上面。 echo 语句在上面的第 33 行左右。
  • @Kepoly 我在 json 调用之后打印到 console.log 一条消息,因此,具有讽刺意味的是,我得到了“成功的 ajax 调用”的声明。下一条语句尝试将响应数据发送到控制台。一个空行被发送到控制台。

标签: javascript php jquery ajax json


【解决方案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");

【讨论】:

  • 谢谢@Jigar,我试试看!
【解决方案2】:

你必须设置contentType,你的内容类型应该是

contentType: "application/json",

你的datatype: "json", 也应该是

 dataType: "json",

【讨论】:

  • 谢谢索兰基。尝试了“application/json”和datatype“json”的contentType,没有结果。上面列出的选项值只是大约 2^4 种不同组合中的最后一个。我希望它是那么容易:-)
猜你喜欢
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多