我正在尝试使用 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/