首页 > 解决方案 > 编码问题 - PHP,MySQL - 奇怪的行为

问题描述

我正在尝试进行 Ajax 调用并以 JSON 格式获取响应。问题是 JSON 获取的字符编码不正确。因此,JSON 格式不正确,会出现错误。

这只发生在我的本地主机中,我已经在服务器上尝试过,它工作正常。我不明白发生了什么。

这是 Ajax 调用:

var getEmployees = function () {
    var name;
    var code;
    var nameCode;
    var result = [];
    $.ajax({
        type: "POST",
        url: "getEmployees.php",
        dataType: 'json',
        success: function (data) {
            var len = data['data'].length;
            for( var i = 0; i<len; i++) {
                name = data['data'][i]['name'];
                code = data['data'][i]['code'];
                nameCode = name + ' - ' + code;
                result.push(nameCode);
            }
            renderEmployees(name);
        },
        error: function (xhr, status, textResponse) {
            console.log(xhr.responseText);
            console.log(status);
            console.log(textResponse);
        }
    });
}

这是 $getEmployees.php 的 PHP 代码

-- Validate database

$getEmployeesQuery = "SELECT e.id, e.name, e.code, e.email, e.active, e.updated_at ";
$getEmployeesQuery .= "FROM emp e ";
$getEmployeesQuery .= "LEFT JOIN ecredit ec ON e.id = ec.employee_id ";
$getEmployeesQuery .= "LEFT JOIN credit c ON c.id = ec.credit_id ";
$getEmployeesQuery .= "LEFT JOIN product p ON p.id = c.product_id ";
$getEmployeesQuery .= "WHERE e.company_id = " .$companyId. " ";
if (isset($searchQuery)) {
    $getEmployeesQuery .= "and (e.name LIKE '%".$searchQuery."%' ";
    $getEmployeesQuery .= "or e.code LIKE '%".$searchQuery."%' ";
    $getEmployeesQuery .= "or e.email LIKE '%".$searchQuery."%') ";
}
if (isset($status)) {
    $getEmployeesQuery .= "and e.active = ".$status." ";
}
$getEmployeesQuery .= "group by e.id ";
$getEmployeesQuery .= "order by e.updated_at desc";

$getEmployees = mysqli_query($dbConnection, $getEmployeesQuery);


if ($getEmployees) {
    while ($row = mysqli_fetch_array($getEmployees)) {
        $getEmployeeCreditsQuery = "SELECT sum(ec.quantity) as quantity, c.expiration_date ";
        $getEmployeeCreditsQuery .= "FROM ecredit ec ";
        $getEmployeeCreditsQuery .= "JOIN credit c ON c.id = ec.credit_id ";
        $getEmployeeCreditsQuery .= "WHERE c.expiration_date > '$currentDate' and ec.employee_id = ".$row['id']." ";

        $getEmployeeCredits = mysqli_query($dbConnection, $getEmployeeCreditsQuery);

        if (mysqli_num_rows($getEmployeeCredits)>0) {
            while ($result = mysqli_fetch_array($getEmployeeCredits)){
                $quantity = $result['quantity'];
            }
        } else {
            $quantity = 0;
        }

        $getEmployeesInfo['data'][] = array(
            'id' => (int)$row['id'],
            'name' => $row['name'],
            'code' => $row ['code'],
            'email' => $row ['email'],
            'status' => (int)$row['active'],
            'quantity' => $quantity
        );

    }

    foreach ($getEmployeesInfo['data'] as $row => $id) {
        $rowIds['meta']['rowIds'][] = $id['id'];
    }

    $mergedArray = array_merge($rowIds,$getEmployeesInfo);

    return print_r(json_encode($mergedArray));

}

以下是数据示例: 数据样本

当数据很小时它工作正常,但是当 JSON 传递超过 65528 个字符时,总是在这个位置得到一个未识别的字符,这是控制台中的 Ajax 响应。

parsererror
tagify-employees.js:26 SyntaxError: Unexpected token  in JSON at position 65528
    at parse (<anonymous>)
    at ajaxConvert (plugins.bundle.js:9013)
    at done (plugins.bundle.js:9483)
    at XMLHttpRequest.<anonymous> (plugins.bundle.js:9785)

我已经查看了所有数据,它不包含任何特殊字符。此外,为 UTF-8 指定了 meta 和 header 标签。这是服务器的配置。

服务器信息

拜托,我不知道为什么会这样,我无法在我的电脑上尝试某些功能。如果有人可以指导我,我将不胜感激。另外,如果您需要更多信息,我可以提供。

标签: phpmysqljsonencoding

解决方案


推荐阅读