WordPress AJAX – how to return true or false in the callback function

So I’ve set up AJAX with WordPress properly, having followed some good tutorials, and it’s working great. One thing I can’t quite work out is how to properly return either TRUE or FALSE from within the callback function.

My php function is as follows:

Read More
function my_callback() {
    if(something()) {
        echo 'false';
    } else {
        echo 'true';
    }
    exit;
}

In the JS do I simply do if(response == 'true') { // do something } or is there a better way?
This might seem a little trivial but this is the first time I’m really using AJAX!

Related posts

Leave a Reply

1 comment

  1. this would work to:

    function my_callback() {
        if(something()) {
            echo 1;
        }
        exit; //no text sent or even exit('0'); will work
    }
    

    and in js:

    var response = do_ajax();
    if(response){
        //do stuff
    }