我见过一些类似的问题,但我找不到解决问题的方法。
<?php
add_action(\'wp_ajax_nopriv_LogHit_callback\', \'LogHit_callback\');
add_action(\'wp_ajax_LogHit_callback\', \'LogHit_callback\');
function HitCount() {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: \'LogHit_callback\',
PostId:\'<?php echo get_the_ID() ?>\'
};
jQuery.post(\'http://www.test.com/wp-admin/admin-ajax.php\', data);
});
</script>
<?php
function LogHit_callback() {
global $wpdb; // this is how you get access to the database
$postId = $_POST[\'PostId\'];
$hits = get_post_meta((int)$postId, \'hit-counter\', true);
$hits = $hits + 1;
update_post_meta((int)$postId, \'hit-counter\', (int)$hits);
exit; // this is required to return a proper result
}
当我在页面中调用HitCount函数时,它会呈现以下内容<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
action: \'LogHit_callback\',
PostId:\'86476\'
};
jQuery.post(\'http://www.test.com/wp-admin/admin-ajax.php\', data);
});
</script>
指向管理ajax的直接链接。php文件在浏览器中工作,但从未调用回调函数?