我正在尝试通过接收充满数据库数据的数组来更新网页。
$("id").load("data.php");
上面的load方法效果很好,但是我需要访问数组才能手动选择在站点上显示数据的位置。
这是我的“ data.php” 文件:
<?php
$db = new SQLite3('db.s3db');
$results = $db->query('SELECT * FROM tb_deviceData');
echo json_encode($results);
这是我的 javascript:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script>
$(document).ready(function()
{
var id = 1;
getData(id);
});
function getData(id)
{
$.ajax(
{
url: 'data.php',
type: 'POST',
dataType:'json',
data: {id: id},
success: function(response)
{
response = JSON.parse(response);
console.log(response[0]);
}
});
}
</script>
如上所示运行脚本时,出现以下错误:
Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at Object.success (index.php:25)
at i (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at A (jquery.min.js:4)
at XMLHttpRequest.<anonymous> (jquery.min.js:4)
但是如果我将代码更改为
success: function(response)
{
console.log(response[0]);
}
错误消失了,我在控制台中以返回的形式得到了“未定义” 字符串值。
我一直在尝试与stackoverflow不同的示例,但似乎没有一个适合我。