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?
wp_ajax_my_action
, the "my_action" part, must match the action used in the jQuery post. I see that yours matches, so that's not the problem in your case. I also have it working from the admin page, which is what you're trying to do. Are you logged in when trying to make this ajax call? Or are you logged out? – jmort253 Jul 6 '12 at 16:13