Reinherd
Reinherd

Reputation: 5506

Ajax and Jquery - Success

I'm using Ajax to call a php site, but I want to do something "on success" and "on error".

By now, this is my js:

$("#votePositive").click(function(){
            var idEntrada = $('.entry').attr('id');
            var url = "{{ path('moderar_entrada_votacion') }}";
            var eleccion=1;
            $.ajax({
                type: "POST",
                url: url, 
                data: {
                   idEntrada: idEntrada,
                   eleccion: eleccion
                },error: function(){alert("eee")}, success: function(){alert("bbb")}
            });  
        });

So this is my php code (just a return, just want to try if the connection works)

return true

But my site popups the alert "eee".

I've tried to:

return false

But same is happening, eee alert is comming up.

If I just remove the "return" statment from the php file, it shows up the "bbb" alert.

How can I deal with those returns? How can I tell Ajax that it went OK or Not?

Upvotes: 2

Views: 75

Answers (1)

juco
juco

Reputation: 6342

It's not to do with returning true or false from your PHP. Instead it's about the HTTP response code and content returned. For example, if your PHP is to simply:

echo 'Hello world'

success will be called. Although you will of course want to return (probably a JSON representation of) your actual dataset.

Upvotes: 1

Related Questions