gpt4 book ai didi

php - 如何将 mysql 数组传递给 jquery php

转载 作者:行者123 更新时间:2023-11-29 15:23:07 25 4
gpt4 key购买 nike

我有一个 PHP 文件,它从数据库表获取数据并将其发送到 jQuery AJAX:

include '../../Connections/Base.php';   

$query = "SELECT codemeli,name,family FROM members ORDER BY id ASC";

if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {

echo json_encode($row);
}
} else {
echo $mysqli->error;
}

jQuery 代码是:

$("#generate").click(function() {
$.ajax({
type: 'POST',
url: "postpon/signup/showMeliCode.php",
success: function (response) {
alert("Before Parse"); // It's work
response = $.parseJSON(response); // don't work
alert(responde); // don't work
}
})

})

如您所知,$.parseJSON 不起作用。为什么 parseJSON 不起作用?

最佳答案

首先,$.parseJSON已弃用。

As of jQuery 3.0, $.parseJSON is deprecated. To parse JSON strings use the native JSON.parse method instead.

您需要输出结果集,而不是单独输出行:

include '../../Connections/Base.php';   

$query = "SELECT codemeli,name,family FROM members ORDER BY id ASC";

if ($result = $mysqli->query($query)) {
echo json_encode($result->fetch_all(MYSQLI_ASSOC));
} else {
echo json_encode(['success' => false, 'message' => $mysqli->error]);
}
$("#generate").click(function() {
$.ajax({
type: 'POST',
url: 'postpon/signup/showMeliCode.php',
contentType: 'application/json; charset=utf-8',
success: function (response) {
console.log(response);
}
});
});

关于php - 如何将 mysql 数组传递给 jquery php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59232908/

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