Ajax only returns if I put an "echo" instead of "Return" in php

Asked

Viewed 1,183 times

1

I am 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 json through Return (I am using a function in php) jquery takes nothing.

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 should really happen? Thank you

  • For a file to be considered a json, its contents must contain a formatting pattern json. When you make a echo you are writing in the file, a Return is just returning a value that can be saved and used somewhere (saved in a variable for example). In short, the echo will fill out the file, the json_encode will leave the data you are using to fill the file in a format json. The echo is used to write to the file, the return nay..

  • @Rickpariz but I’ve seen applications that use Return to return the values of the ajax request and work normally, so I’m surprised.

  • Depending on the structure (if it is a Framework I know there), you give only a Return and at the end of everything the structure calls some method to print on the screen.. I don’t know.. I’ve never particularly used and never seen Return used to return data for an ajax request.

  • Oh I get it, it’s probably happening so thank you!

  • The Return does not display the values, for example, if you use the Laravel, you have to use a function to return the json return response()->json($return);

3 answers

2

Well, the ajax call reads the server response and that response should be processed as some kind of readable data, like application/jsonou or text/html.

To write this data, you need echo server with PHP.

The return statement does not write data, it simply returns at the server level.

answered on the site in English

2


To communicate from PHP to javascript you need to do it through a text output (echo/print) or other functions (remember http is a text-based protocol). The most correct is to leave the Return at the end of the method and at the time of calling draw() add echo.

Can simplify the method for:

public static function draw(){
    return  json_encode(["sucesso" => false, "redireciona" => false, "mensagem" => "Dados incorretos"]);
}

When it’s time to call:

echo classe::draw();

2

According to http://php.net/manual/en/function.return.php the return is a PHP language control structure, so use it exclusively in PHP to return values or return to the next run point.

The echo, in accordance with http://php.net/manual/en/function.echo.php is characterized as a string function (although it does not behave like a function). What it does is write the strings in the output buffer.

In your case jQuery calls PHP through your HTTP server waiting for a json output from this HTTP server. And to do this you must write in the output buffer, so this justifies why the echo works and the return nay.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.