php - 连接postgres和php,使用d3.js可视化

标签 php json postgresql d3.js

我正在尝试使用 php 连接 postgres 并使用 d3.js 可视化数据。我成功地将数据编码为 json,但是当我想使用 d3.js 加载数据时,出现 SyntaxError: Unexpected token < in JSON at position 0(...) index.html:12 未定义。

这是我的 php 代码:

$db_connection = pg_connect("host=localhost dbname=xxx user=xxx     password=xxx");
    $result = pg_query($db_connection, "SELECT * FROM taxi_stats.satisfy");
    $data = array();
    while ($row = pg_fetch_array($result))
    { 
      $data[] = $row;
    } 
    echo json_encode($data);
    pg_close($db_connection);

对于 d3.js

<script>
d3.json("data.php", function(error, data) {
    if(error){
        console.log(error);
    }
    console.log(data);
});
</script>

最佳答案

将 header 内容类型从“text/javascript”更改为“text/html”。

或者可能是标记“>”之一没有用单引号或双引号引起来的错误。

关于php - 连接postgres和php,使用d3.js可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37218785/

相关文章:

PHP 重定向 - (SNI) 错误定向请求客户端需要一个新的连接 - 主机与服务器名称指示不匹配

postgresql - 日期范围内每个日期的汇总计数

mysql - 是否真的需要在所有使用它的表中将列定义为外键

javascript insidehtml 不会将变量发送到链接

php - zii.widgets.CDetailView 格式化日期

php - MySql 组合结果并应用于第二个表

java - 我如何转换这个字符串,它是java集合中的json响应?

java - 将 JSONArray 转换为字符串数组

java - 将 JSONObject 解析为自定义类

node.js - 安装pg Node 模块时出错