I'm building an application using ajax (jquery) and php, when returning a json by php, jquery only takes the value if I have given an ECHO, if I return the json through the return (I'm using a function in php ) jquery does not catch anything.
jquery:
$.ajax({
url: BASE_URL+action,
data: $(this).serialize(),
type: "POST",
dataType: 'json',
success: function(result){
if(result.sucesso && !result.redireciona){
sucesso(result.mensagem);
}
if(!result.sucesso && !result.redireciona){
erro(result.mensagem);
}
if(result.redireciona)
{
location.href = result.link;
}else {
botao.prop("disabled", false).val(textBotao).css('opacity', '1');
}
},
error: function(){
botao.prop("disabled", false).val(textBotao).css('opacity', '1');
}
});
php:
public static function draw(){
$return = ["sucesso" => false, "redireciona" => false, "mensagem" => "Dados incorretos"];
return json_encode($return);
}
If I replace return json_encode ($ return) with echo json_encode ($ return) works normally
Can anyone tell me if this really should happen? Thank you