0

I'm getting the error 'Unexpected end of JSON input' when using JSON to retrieve data from a MySQL db
The following code snippet calls GetProfileTest.php which executes a select statement and returns a record. The record contains the field value 'Athlète'. If delete è from the field the codes runs without an error. The db is uses utf8_unicode_ci collation.

The weird thing is the code works fine on another website with identical tables.

Can anybody provide an insight has to what causes the error. Eliminating the French characters is not an option.

    $.ajax({
    method: 'GET',
    url: '/wp-content/PHP/GetProfileTest.php',
    data :{"CFF":CFF,"LANG":LANG},
    "timeout": 5000
})  
.fail(function (jqXHR, textStatus, errorThrown) {
    // Request failed. Show error message to user.
    alert('Data get failed: '+errorThrown);
})
.done(function(data, textStatus, jqXHR ) {
    var result= data;
    if ($.type(result)=="string") {
        alert("No profile found " + CFF); 
    };

Here is GetProfileTest.php

<?php 

// Revieve ID and query db to get profile

// get connection data)
require('Connection.php');

/* Create connection */
$conn =  mysqli_connect($host,$username, $password, $dbname);

// Check connection
if (!$conn){
  echo 'Connection error: '. mysqli_connect_error();
  }
 
 //  SQL query to get profile for the ID provided
 $CFF  = $_GET['CFF'];
 $LANG = $_GET['LANG'];

 if ($LANG == 'EN') {
    $sql = "SELECT CFF_Num,VeteranName,Role_En FROM VeteranProfileView WHERE CFF_Num='".$CFF."'";
 }  else {
    $sql = "SELECT CFF_Num,VeteranName,Role_Fr FROM VeteranProfileView WHERE CFF_Num='".$CFF."'";
 }
   $sql = "SELECT CFF_Num,VeteranName,Role_Fr FROM VeteranProfileView WHERE CFF_Num='".$CFF."'";
 
 // Run Query and get result
 if (!$result = mysqli_query($conn,$sql)) {
    echo 'Query failed: '. mysqli_error($conn);
 }

 /* If there are results from query push to result array */
 $result_array = array();
 if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
       array_push($result_array, $row);
    }   
    /* send a JSON encoded array to client */
    header('Content-type: application/json');
    echo json_encode($result_array);
 } else {
    echo ('No profile found');
 } 


 mysqli_free_result($result);
 mysqli_close($conn);
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    try to convert specific field type to utf8mb4_general_ci. – Ujjaval Feb 08 '21 at 14:11
  • This is an encoding problem. *"The db is uses utf8_unicode_ci collation."* What does your PHP response say it is? It should say that it's UTF-8, so that the browser knows to use UTF-8 when reading the response and building the string to pass to JavaScript. Apparently, your PHP response is being interpreted in a different encoding (probably [ISO-8859](https://en.wikipedia.org/wiki/ISO/IEC_8859-1) or [Windows-1252](https://en.wikipedia.org/wiki/Windows-1252), but it could be anything your server defaults to). – T.J. Crowder Feb 08 '21 at 14:12

0 Answers0