Skip to content
Advertisement

Json corrupt variable by addition of a character

I make an Ajax call to a server side php script.

The php should return a json variable like

this : {“erreur”:”Pas de diffusion possible avant le 20-11-2020″,”title”:”Diffusion”}

And I got that : s{“erreur”:”Pas de diffusion possible avant le 20-11-2020″,”title”:”Diffusion”}

Where does this ‘s’ come from ?

A chunk of my code on the server side (‘DiffuseOffre.php’):

 $nextdate=date("d-m-Y", time() + 24 * 60 * 60);
    $message = "Pas de diffusion possible avant le " . $nextdate;
    $title = "Diffusion";
    $data_php = array(
        "erreur" => $message,
        "title" => $title
    );
    $retour = json_encode($data_php);
    $error=json_last_error();
    echo $retour;
    exit();

The javascript on the client side :

function DiffuseOffre(envoi, tab, paquet, dest) {
    var server ='/Admin/Offres/DiffuseOffre.php';
    $.ajax({ 
        url: server,
        type:'Post',
        dataType: 'json',       
       data: {
            envoi: envoi,
            tab: tab,           
            paquet:paquet,
            dest: dest      
       }    
     }).done(function(response) {
        alert(response);        
       if (response.hasOwnProperty('erreur')) {
            $("#dialog-erreur").html(response.erreur);
            $("#dialog-erreur").dialog("open");
            $("#dialog-erreur").dialog({                
                width: '600px',
                title: response.title           
            });                 
       } else {     
                ....
        }
    }); 
}

The php debuger show me a correct syntax of the json encoded $retour :

{"erreur":"Pas de diffusion possible avant le 20-11-2020","title":"Diffusion"}

On the firefox debugger ‘response’ appears as ‘undefined’ and in the console :

Uncaught SyntaxError: unexpected token: identifier
    DiffuseOffre http://localhost/Admin/Util/js/main.js:336
    onclick http://localhost/Admin/Offres/diff_offre.php:1

I look for a sticky ‘s’ which would have been paste to my $retour or $message var, but nothing !

Did someone knows how to remove that ‘s’ character ?

Advertisement

Answer

Are you just trying to find what’s wrong with your JSOn file? If so, you could copy/paste it in https://jsoneditoronline.org/# and you’ll see right away where that extra character is located at.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement