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

标签 php jquery mysql ajax parsing

我有一个 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/

相关文章:

php - PHP 中 rename() 的奇怪行为

javascript - jvectormap 区域颜色

php - 使用 PHP,在搜索中如何在来自 MySql 数据库的图像中包含 html 链接?

javascript - 需要显示 Toastr 通知 jquery 插件

php - 如何将同一个表中的多个用户信息回显并分离到他们的每个页面

javascript - 成功功能在ajax中不起作用

javascript - 如何使用angular js在母版页中设置事件菜单

mysql - jTable 在使用 node.js 进行 MySQL INSERT 后不显示新行内容

PHP避免重复查询结果问题

PHP如何截断数组