gpt4 book ai didi

javascript - response.forEach 不是 ajax 函数

转载 作者:行者123 更新时间:2023-11-29 17:52:02 26 4
gpt4 key购买 nike

使用ajax通过php和javascript从mysql中提取数据,我得到了一个函数错误:response.forEach不是一个函数,我看到很多人都发布了这个问题,但他们的解决方案都没有帮助我,或者我写错了,我添加了 php 和 ajax 代码以及正在检索的数组。

数组结果:

{query1: Array(6), query2: Array(17)}
query1
:
Array(3)
0
:
{beacon: "81", location: "YELLOW", date: "2018-03-12", counter: "40"}
1
:
{beacon: "69", location: "YELLOW", date: "2018-03-12", counter: "39"}
2
:
{beacon: "257", location: "YELLOW", date: "2018-03-12", counter: "23"}
:
__proto__
:
Array(0)
query2
:
Array(5)
0
:
{beacon: "10", location: "YELLOW", delivery_avg: "01.38"}
1
:
{beacon: "101", location: "YELLOW", delivery_avg: "10.36"}
2
:
{beacon: "111", location: "YELLOW", delivery_avg: "23.48"}
3
:
{beacon: "119", location: "YELLOW", delivery_avg: "00.06"}
4
:
{beacon: "16", location: "YELLOW", delivery_avg: "00.00"}
length
:
4
__proto__
:
Array(0)
__proto__
:
Object

ajax代码:

   $.get('php/test.php', function(response) {
console.log(response);
var row;
response.forEach(function(item, index) {
console.log(item);
$(`td.${item.beacon}`).css('background-color', item.location).toggleClass('coloured');
});
});

PHP 代码:

$sql1 = 'SELECT 
*
FROM
(SELECT
beacon,location,date,
COUNT(location) AS counter
FROM `test`.`test`
WHERE `date` = CURDATE() and `time` > NOW() - interval 40 second
GROUP BY beacon) AS SubQueryTable
ORDER BY SubQueryTable.counter DESC;';
$result1 = $conn->query($sql1);
$rows1 = $result1->fetch_all(MYSQLI_ASSOC);

$sql2 = "SELECT beacon,location,TIME_FORMAT(TIMEDIFF(max(`time`),min(`time`)), '%i.%s')
AS `delivery_avg`
FROM `test`.`test`
where date = CURDATE()
and time > now() - INTERVAL 30 MINUTE
group by beacon";
$result2 = $conn->query($sql2);
$rows2 = $result2->fetch_all(MYSQLI_ASSOC);


$result = array(
'query1' => $rows1,
'query2' => $rows2,
);

echo json_encode($result);

最佳答案

你可以使用它

$.each(object, function(index, value) {
console.log(value);
});

关于javascript - response.forEach 不是 ajax 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49234259/

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