WordPress Ajax returning 0

I hope you can help.
The following code in functions.php is returning 0.

function removeItems(){
echo "hello";
die();
}

add_action('wp_ajax_removeItem', 'removeItems');
add_action('wp_ajax_nopriv_removeItem', 'removeItems'); 

function remove_item(){
  echo '<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("body").delegate(".remove_one","click",function(){
            var cart_key = jQuery(this).data("cart_key");
            jQuery.ajax({
                type:"POST",
                url: "/wp-admin/admin-ajax.php",
                data: {action: "removeItem"},
                success:function(data){
                    alert(data);
                }
            });
        });
    });
</script>';
}

add_action('wp_head', 'remove_item');

The common error I can find is not including:

Read More
add_action('wp_ajax_nopriv_removeItem', 'removeItems'); 

…but I’ve added that in.

action=removeItem

is being added in the console.

Any help would be much appreciated.
Mark

Related posts

Leave a Reply

1 comment

  1. If an action is not specified, admin-ajax.php will exit, and return 0 in the process. This means your frontend has fire an action which WordPress not recognizing. WordPress cannot find your wp_ajax_xxx action. You may have specified this action, but remember to load this file, so that it is being called.

    Summary:

    1. In your plugin file/themes, remember to load the file where the
    add_action('wp_ajax_removeItems', 'removeItems');  
    function removeItems() {     
            ...;     
           echo json_encode($array);    
    }
    
    1. Call them from your front-end
    (function($){  
    var  ret     = true,  
      ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>',  
      data    = {  
       'action': 'removeItems',  
      };   
    
    $.post(ajaxurl, data, function(response) {  
        console.log(response);  
      });  
    })(jQuery);