可能重复:
如何将 json 字符串发送回 jquery
我需要运行myquery.php,然后我需要在 JavaScript 函数createChartControl中使用这个 php 代码的输出。下面是我的完整代码。alert(returned_array)
问题是没有出现Alert 消息 ( ),并且函数createChartControl没有执行。我单独测试了 myquery.php,它工作正常。
<script>
function updateList(){
$.ajax({
url: "myquery.php",
type: 'GET',
data: { query: "SELECT tm FROM schedule WHERE val='BT009';" },
dataType: 'json',
success: function (data) {
var returned_array = $.parseJSON(data);
alert(returned_array);
createChartControl('schedule', returned_array);
}
})
}
$(document).ready(function() {
updateList();
});
</script>
<script type="text/javascript" language="JavaScript">
function createChartControl(htmlDiv1,data)
{
//...
}
</script>
<?php
include_once 'include/connect_db.php';
$query = $_GET['query'];
$condb = connectDB();
$result=execute_query($query);
closeDB($condb);
$rows = array();
while($row = mysql_fetch_array($result)) {
$rows[] = $row['tm'];
}
return $rows;
?>
更新:仍然说:你的 ajax 刚刚失败
function updateList(){
$.ajax({
url: 'myquery.php',
type: 'POST',
data: {'query': "SELECT tm FROM schedule WHERE val='BT009';" }
}).done(function(data) {
console.log(data);
// createChartControl('schedule', returned_array);
}).fail(function() {
console.log('Your ajax just failed');
});
}
我的查询.php
<?php
include_once 'include/connect_db.php';
if (isset($_POST['query'])) {
$query = $_POST['query'];
echo($query);
$condb = connectDB();
$result=execute_query($query);
closeDB($condb);
$rows = array();
if ($result && mysql_num_rows($result)) {
while($row = mysql_fetch_array($result)) {
$rows[] = $row['tm'];
}
}
echo json_encode($rows);
}
?>