如何解决编码问题-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);
}
});
}
-- 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));
}
以下是数据示例: data sample
当数据较小时,它可以正常工作,但是当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标签。这是服务器的配置。
请,我不知道为什么会这样,并且我无法在计算机上尝试某些功能。如果有人可以指导我,我将不胜感激。另外,如果您需要更多信息,我可以提供。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。