Admin ajax request return 0 with die()

So i’m using this configuration for an AJAX call in the administrative area (like this):

add_action( 'admin_head', 'ajaxPerNgg' );

function ajaxPerNgg(){ ?>
<script type="text/javascript" >
jQuery(document).ready(function() {
    jQuery("#message").remove();
    jQuery("#errore a#attivo").click(
        function(){
            var data = {
                action: 'my_action'
            };
            jQuery.post(ajaxurl, data, function(response) {
                if(response == "attivato")
                    jQuery("#errore a#inattivo").attr("id","completato").html("Attivato");
                else
                    jQuery("#errore a#inattivo").attr("id","attivo").html("Attivalo ora");
            });
        }
    );
});
</script>
<?php }

add_action('wp_ajax_my_action', 'my_action_callback');

function my_action_callback() {
    echo 'attivato';
    die();
}

It’s just a test code, but it always return 0 instead of the string “attivato”. It also find the ajax url correctly, without a 404 error.
What is the problem?

3 s
3

Add this as well: add_action('wp_ajax_nopriv_my_action', 'my_action_callback');. Notice the nopriv.

It will solve your problem!

Leave a Comment