php - 谷歌可视化 API php ajax

标签 php javascript mysql google-visualization

我正在使用这个Achieving Google Visualization chart reloads using ajax从 Mysql 数据库动态检索数据的示例,我收到“Uncaught SyntaxError: Unexpected token <”错误。 这是我的 HTML 文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("jquery", "1.6.1");
      google.load('visualization', '1', {packages: ['table']});
    </script>
    <script type="text/javascript">

    function drawVisualization(dataFromAjax) {
        var data = new google.visualization.DataTable();
        data.addColumn('number', 'InvoiceNo');
        data.addColumn('string', 'B/L No');
        data.addColumn('date', 'Date');
        data.addColumn('string', 'Customer Name');
        data.addColumn('number', 'Amount');
        data.addRows(dataFromAjax);
        var table = new google.visualization.Table(document.getElementById('table'));

        table.draw(data);
   }

    function makeAjaxCall() {
      $.ajax({url:'test.php',
              data: {},
              success: function(responseData) {

                         var arrayForGviz = eval("(" + responseData + ")");
                         drawVisualization(responseData);
                       }
        });
    }
    </script>
  </head>
  <body>
    <input type="button" onclick="makeAjaxCall();return false;" value="Click to get data"></input>
    <div id="table"></div>
  </body>
</html>

这是我的 PHP 文件 test.php



    $con = mysql_connect("localhost","userName","password");

    if (!$con)

    {

    die('Could not connect: ' . mysql_error());

    }

    mysql_select_db("DB_NAME", $con);

    $result = mysql_query("call cargosys.rpt_salesByDate('2013/03/05','2013/03/10')");
    $output = array();
    while($row = mysql_fetch_array($result)) {
        // create a temp array to hold the data
        $temp = array();

        // add the data
        $temp[] = $row['inv_no'];
        $temp[] = ''' . $row['bl_no'] . ''';
        $temp[] = ''' . $row['inv_date'] . ''';
        $temp[] = ''' . $row['cust_name'] . ''';
        $temp[] = $row['Amount'];

        // implode the temp array into a comma-separated list and add to the output array
        $output[] = '[' . implode(', ', $temp) . ']';
    }

    // implode the output into a comma-newline separated list and echo
    echo implode(",\n", $output);
    //echo json_encode($output);
    mysql_close($con);

最佳答案

在成功函数中执行以下操作:

console.log(responseData);

然后将responseData复制并粘贴到jsonlint.com上的JSON验证器中

我猜这是一个 JSON 语法错误,验证器会向您指出。

关于php - 谷歌可视化 API php ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15337391/

相关文章:

php - 如何在 'select mysql query' 的每个不同结果中提交包含答案的表单,并将答案插入 mysql 数据库

php - PHP 文件名(或其完整路径中的目录)可以包含 UTF-8 字符吗?

javascript - 查看 Canvas 是否有任何数据(黑色除外)

java - 没有定义名为 'persistenceUnit' 的 bean

mysql - 在 MySQL 中使用 Group by 和 Order by

php - 如何将帖子(变量)信息传递到第二页

php - 如何用 php 显示 mdb(access) 文件字段(包括 utf8(阿拉伯波斯语)字符)?

javascript:参数值更改会导致参数值更改吗?

javascript - 应用程序崩溃并显示 'Accessing object of type which has been invalidated or deleted' 错误消息?

mysql - 从 Mariadb 中的动态列字段中提取嵌套对象