下面是我尝试做的:在我的主题函数中调用函数。php从我的主题前面。
因此,在前面:
$(document).on(\'click\',\'.btn-vote\',function(e){
e.preventDefault();
var $that = jQuery(this);
var id = $that.attr(\'data-id\');
var data = {
\'action\': \'voteIncrementer\',
\'id\': id
};
jQuery.post(ajaxurl, data,function(response){ // ajaxurl is defined in footer.php > var ajaxurl = \'<?php echo admin_url(\'admin-ajax.php\'); ?>\';
console.log(response);
$that.find(\'.vote-count\').html(response);
});
});
以及函数中的相关函数。php:add_action("wp_ajax_voteIncrement", "voteIncrement");
add_action("wp_ajax_nopriv_voteIncrement", "voteIncrement");
function voteIncrement(){
echo \'hello\';
die();
}
退出简单对吗?但问题是:在我的JS中,日志返回0(这意味着wp ajax找不到我的函数)。我错过了什么?