0

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.

Server info

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.

11
  • 1
    Why print_r?? Commented Oct 19, 2020 at 21:19
  • I have tried echo and printf but no success as well. Any suggestions? Commented Oct 19, 2020 at 21:22
  • 1
    No idea, depends what the rest of your code is like. Just seemed odd. Have you tried dumping out the JSON in PHP and checking that it's valid before it gets to the JS? Commented Oct 19, 2020 at 21:23
  • 1
    stackoverflow.com/questions/26698623/… might be relevant. Unsure. Commented Oct 19, 2020 at 21:25
  • Given the complication only occurring locally at a certain file length, do you know if your local environment's php settings (i.e. upload_max_filesize and post_max_size in your .ini) are the same as the server you've tested on? Commented Oct 19, 2020 at 21:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.