Home » Php » javascript – Why using ajax a php file return empty string and works only if i add print_r?

javascript – Why using ajax a php file return empty string and works only if i add print_r?

Posted by: admin February 25, 2020 Leave a comment

Questions:

My friends i have a php file that return a number and this file end in this way:

$decrypted= 4;
$numero_iscritti= json_encode($decrypted); //numero di iscritti
return $numero_iscritti;    //return 

In js file i use ajax to get $numero_iscritti

success: function (result) { //se funziona
        var numero_iscritti = JSON.parse(JSON.stringify(result));   
}

but numero_iscritti (is global in my code) is empty !

infact with

error: function(ts) {
        console.log(ts.responseText);
}

i have empty string but if inside php file, before the return, i add a print_r then success function work well.

 $decrypted= 4;
 $numero_iscritti= json_encode($decrypted); //numero di iscritti
 print_r($numero_iscritti);
 return $numero_iscritti;   //return 

Why ?

How to&Answers: