gpt4 book ai didi

javascript - JSON $ajax 问题

转载 作者:行者123 更新时间:2023-12-03 04:07:11 26 4
gpt4 key购买 nike

我尝试从 JSON 文件制作谷歌图表,但看不到错误。这是我的 php,它执行 JSON 数据文件。php 代码工作得很好,它给了我这个:

[["Duratapedeapsa","count"],["3","4"],["7","2"],["5","1"],["2", "2"],["4","1"]]

但是我无法将其加载到 html 中。

<?php
//Oracle DB user name
$username = 'TW';

// Oracle DB user password
$password = 'TW';

// Oracle DB connection string
$connection_string = 'localhost/xe';

//Connect to an Oracle database
$connection = oci_connect(
$username,
$password,
$connection_string
);

$stid = oci_parse($connection, 'SELECT to_number(substr(durata_pedeapsa,0,1)) as "pedeapsa" , COUNT(DURATA_PEDEAPSA) as "count" FROM DETINUTI group by substr(durata_pedeapsa,0,1)');
if (!$stid) {
$e = oci_error($connection);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$r = oci_execute($stid);
if (!$r) {
$e = oci_error($stid);
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

# set heading
$data[0] = array('Durata pedeapsa','count');
$i=1;
while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_LOBS)) != false) {
$pedeapsa = $row['pedeapsa'];
$count = $row['count'];
$data[$i] = array($pedeapsa,$count);
$i = $i +1;
}
echo json_encode($data);
oci_close($connection);
?>

这是我应该创建图表的 html。

<html>
<head>
<title>Suji</title>
<!-- Load jQuery -->
<script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js">
</script>
<script src="jquery.min.js"></script>
<script src="jquery-ui.min.js"></script>
<!-- Load Google JSAPI -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);

function drawChart() {
var jsonData = $.ajax({
url: "/test.php",
dataType: "json",
async: false
}).responseText;

var obj = window.JSON.stringify(jsonData);
var data = google.visualization.arrayToDataTable(obj);

var options = {title: 'Suji'};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}

</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;">
</div>
</body>
</html>

最佳答案

使用$.ajax()的成功回调方法将数据加载到图表中,并且不要调用JSON.stringify(),因为Google图表接受jquery对象而不是json字符串。您的 drawChart() 函数应如下所示。

function drawChart() {
$.ajax({
url: "/test.php",
dataType: "json",
success:function (res) {
var jsonData = res.responseText;
var data = google.visualization.arrayToDataTable(jsonData);
var options = {title: 'Suji'};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
});
}

我希望它能工作并且你的 php 脚本返回有效的 json。

关于javascript - JSON $ajax 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44499177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com