0

I have an issue with encoding json to sent to datatable.net. I received DataTables warning: "table id=varietyTable - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1." Checked the development tool. I found out that there is no response. There apparently there is something wrong with json_encode. I look at the record. it seems to have problem handing ascii code 160. The same code running on my local machine (running xampp). There is no problem.

I tried to echo the page. I see unreadable character on the remote server but no local host. output on remote server: Alma�� output on local host: Alma (note that there are spaces behind the word Alma).

The php code are identical. I am just wondering whether the local php setting is different so that the special characters can handle properly.

Thanks.

here are some code from php code that receiving the records and return as Json. The code is rather long. I am posting the parts the fetch the records and return as json. The error message is already posted above: Invalid Json response (in fact. there is no response). It happens whenever I navigate to the records containing the special characters. Again, I only got the error on the remote host, not on the local host. That leads me believe that it's the configuration issue because the source code on the localhost and remote machine are identical.

    $totalQuery = mysqli_query($conn, $sql);
    $totalRowCount = mysqli_num_rows($totalQuery);

    if($_POST['length'] != -1){
        $start = $_POST['start'];
        $length = $_POST['length'];
        $sql .= " LIMIT " . $start . ", ". $length;
    }
    
    $filteredQuery = mysqli_query($conn, $sql);
    $filteredRowCount = mysqli_num_rows($filteredQuery);

    $data = array();

    $counter;
    while($row = mysqli_fetch_assoc($filteredQuery))
    {
        $counter += 1;
        $sub_array = array();
        $sub_array[] = $row['variety_name'];
        $sub_array[] = $row['abbreviation'];
        $sub_array[] = $row['fig_family_name'];
        $sub_array[] = $row['variety_id'];
        $data[] = $sub_array;
     
    }

    $output = array(
        'draw'=> intval($_POST['draw']),
        'recordsTotal' =>$totalRowCount,
        'recordsFiltered'=>$totalRowCount,
        'data'=>$data,
    );
    
    echo  json_encode($output);
  • 1
    Could you please provide some code and any error messages that you've received. Thanks. – jhmckimm Dec 28 '21 at 04:33
  • See "black diamonds" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored Your problem may also involve "double encoding". JSON must be UTF-8; that any non-Ascii characters must be encoded that way. – Rick James Dec 28 '21 at 07:13
  • The data was imported from spreadsheet that might shared across the users from different countries. There might be some Spanish and French. There are also some blank spaces after the text. i use mysql ascii function. They appears to be ascii code 160 (rather than 194 as I previously posted). – user3294653 Dec 28 '21 at 20:37
  • I digging in a little bit more. the mbstring was disabled on my remote server. I enabled it but got the error even on the first load. I run mb_detect_encoding($row['variety_name'], "auto") . it return nothing. It looks like php has trouble detecting it's encoding. – user3294653 Dec 28 '21 at 21:14

1 Answers1

-1

I enabled mbstring on my remote server. The error went away.

I noticed additional problem with the characters that is not handled and had to use $newName = mb_convert_encoding($string, "UTF-8", "ISO-8859-1") to force the conversion from ISO-8859-1 to UTF-8. Now that the error completely gone.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 28 '21 at 23:20