Ajax responseText URL不起作用

y

我正在尝试查询mysql db并将结果放在图表中。它有两个文件index.phpgetpiechartdata.php这两个文件都在我的远程服务器上的/public_html/文件夹中。

我不确定是否index.php正在调用我的getpiechartdata.php脚本。尽管从返回的数据中(直接调用data.php时)getpiechartdata.php看起来像这样。

{ "cols": 
  [ {"id":"","label":"Libraries","pattern":"","type":"string"}, 
    {"id":"","label":"Count","pattern":"","type":"number"} ], 
  "rows": [ {"c":[{"v":"/usr/lib64/crti.o","f":null},
                  {"v":77005,"f":null}]}, 
            {"c":[{"v":"/usr/lib64/crtn.o","f":null},
                  {"v":77005,"f":null}]}, 
            {"c":[{"v":"/lib64/ld-2.11.3.so","f":null},
                  {"v":53922,"f":null}]}, 
            {"c":[{"v":"/lib64/libc-2.11.3.so","f":null},
                  {"v":53922,"f":null}]}]
}

我不确定如何测试对我的PHP脚本的url:ajax调用是否正常工作。如果有人可以给我一些指导,我将感到高兴。

<html>
<head>
<!--Load the AJAX API-->
  <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  <script type="text/javascript" src="jquery-1.9.1.min.js"></script>
  <script type="text/javascript">

// Load the Visualization API and the piechart,table package
   google.load('visualization', '1', {'packages':['corechart','table']});

  function drawItems(syshost) {

          // document.write(syshost);

          var jsonPieChartData = $.ajax({url: "getpiechartdata.php",data: "q="+syshost,dataType:"json", async: false}).responseText;

          // Create our data table out of JSON data loaded from server.
          var piechartdata = new google.visualization.DataTable(jsonPieChartData);

          // Instantiate and draw our pie chart, passing in some options.
          var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
          chart.draw(piechartdata, {
          width: 700,
          height: 500,
          chartArea: { left:"5%",top:"5%",width:"90%",height:"90%" }
          });
  }
   </script>
</head>

<body>
<h1> Welcome to UTKA! </h1>

  <form>
  <fieldset>
  <legend>Most Used Libraries Till Date</legend>
  <p>Select Syshost
  <!-- <select name="users" onchange="drawItems(this.value)">   -->
  <select name="users" onchange="drawItems(this.options[this.selectedIndex].value)">

<?php

try {
    include (__DIR__ ."/include/conn.php");
    $conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "SELECT DISTINCT(build_syshost) AS syshost  
            FROM utka_link
            ORDER BY syshost ASC";

    $query = $conn->prepare($sql);
    $query->execute();

    foreach($query->fetchAll(PDO::FETCH_ASSOC) as $row){
        echo '<option value='. $row['syshost'] . '>'. $row['syshost'] . '</option>';
    }
}
catch(PDOException $e) {
    echo "Error: " . $e->getMessage();
}
$conn = null;
?>
</select></fieldset></form>
</body>
</html>

getpiechartdata.php

<?php
//  $q=$_GET["q"];
  $q='darter';;
try {
        include (__DIR__ ."/include/conn.php");
        $conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT object_path AS Libraries, module_name AS ModuleName, count(date) AS Count 
                FROM utka_link, join_link_object, utka_object 
                WHERE build_syshost='$q' AND 
                utka_link.link_id = join_link_object.link_id AND 
                join_link_object.obj_id = utka_object.obj_id 
                GROUP BY object_path 
                ORDER BY Count DESC
                LIMIT 10";

        $query = $conn->prepare($sql);
        $query->execute();

        $result = $query->fetchAll(PDO:: FETCH_ASSOC);

        echo "{ \"cols\": [ {\"id\":\"\",\"label\":\"Libraries\",\"pattern\":\"\",\"type\":\"string\"}, {\"id\":\"\",\"label\":\"Count\",\"pattern\":\"\",\"type\":\"number\"} ], \"rows\": [ ";

        $total_rows = $query->rowCount();
        $row_num = 0;

        foreach($result as $row){
                $row_num++;
                if ($row_num == $total_rows){
                        echo "{\"c\":[{\"v\":\"" . $row['Libraries'] . "\",\"f\":null},{\"v\":" . $row['Count'] . ",\"f\":null}]}";
                } else {
                        echo "{\"c\":[{\"v\":\"" . $row['Libraries'] . "\",\"f\":null},{\"v\":" . $row['Count'] . ",\"f\":null}]}, ";
                }
        }
        echo " ] }";
}
catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
}
$conn = null;
?>
佩莱格

一种简单的查看幕后情况的方法是使用浏览器中的Web检查器(开发工具)。

  1. 在Google Chrome浏览器中,打开开发工具(Ctrl+ Shift+ I/ Cmd+ Opt+ I),然后单击Network选项卡。
  2. 调用您的Ajax请求(打开“开发工具”面板)
  3. 单击刚刚添加到列表中的请求并检查它。请密切注意HeadersResponse选项卡。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何显示ajax responseText?

来自分类Dev

为什么$ .ajax({url:“ xxx”})。responseText返回未定义?

来自分类Dev

Grails Ajax URL createLink不起作用

来自分类Dev

Grails Ajax URL createLink不起作用

来自分类Dev

ajax.responseText 为空

来自分类Dev

Ajax不起作用

来自分类Dev

$ ajax url的Ajax和jQuery调用不起作用

来自分类Dev

正确显示ajax error.responseText

来自分类Dev

ajax responseText显示为文本,而不是html

来自分类Dev

ajax responseText包含php源代码

来自分类Dev

无法从Ajax请求中获取responseText

来自分类Dev

ajax responseText显示为文本,而不是html

来自分类Dev

jQuery AJAX responseText返回未定义

来自分类Dev

如何获取Polymer is-ajax提交的responseText?

来自分类Dev

Ajax承诺不起作用

来自分类Dev

Ajax形式不起作用

来自分类Dev

Ajax测试不起作用

来自分类Dev

Ajax链接不起作用

来自分类Dev

Ajax的代码不起作用

来自分类Dev

Ajax代码不起作用

来自分类Dev

Ajax更新不起作用

来自分类Dev

Ajax形式不起作用

来自分类Dev

Ajax发布不起作用

来自分类Dev

AJAX方法不起作用

来自分类Dev

Ajax查询不起作用

来自分类Dev

AJAX 评论系统,ajax 不起作用

来自分类Dev

Python Flask SocketIO基本应用程序不起作用-无法从“ XMLHttpRequest”读取“ responseText”属性

来自分类Dev

为什么 .getjson 不起作用而 .ajax 起作用?

来自分类Dev

Ajax在Wordpress管理中不起作用