I am trying to make an Ajax call and get the response in JSON. The problem is that the JSON gets characters that are not encoded correctly. Therefore, the JSON is not correctly formed and gets and error.
This only happens in my localhost, I have tried it in the server and it works fine. I don't understand what is happening.
Here is the Ajax call:
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);
}
});
}
Here is the PHP code for $getEmployees.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));
}
Here is an example of the data: data sample
When the data is small it works fine but when the JSON passes over 65528 characters, always gets an unidentified character in this position, this is the Ajax response in the console.
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)
I have reviewed all the data and it doesn't contain any special characters. Also, the meta and header tags are specified for UTF-8. Here are the configurations for the server.
Please, I don't know why this is happening and I cannot try certain features on my computer. I would greatly appreciate it if someone could guide me. Also if you need more info, I can provide it.
print_r??upload_max_filesizeandpost_max_sizein your .ini) are the same as the server you've tested on?