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 ?
2
Answers
I got it finally. In my complete php code I include a file with different parameters. This is also a php file. After the last closing tag (?>) I found the 's' character. Due to its position (out of the php tags) it don't generate any syntax error in my IDE. Its probably the result of a lacked Ctrl+s
This does not explain, for me, why I found it paste to the returned values (!)
Just a complete day lost :-)
Sorry.
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.