weixin_33744141 2014-04-23 09:46 采纳率: 0%
浏览 33

Wordpress Ajax返回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:

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

  • 写回答

1条回答 默认 最新

  • Lotus@ 2015-03-18 09:02
    关注

    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); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作