I'm getting trying to learn some AJAX for WordPress yet getting stumped at what seems like it should be simple. AJAX function gets the ID correctly, sends it via admin-ajax.php (which I can see in XHR tab of devtools) but function won't receive it. What am I missing please?
jQuery(document).ready( function($) {
var post_id = $('article').attr('ID');
post_id = post_id.replace('post-','');
console.log(post_id);
$.ajax({
url: updatecount.ajax_url,
data : {
action : 'updateCount',
post_id : post_id,
},
success : function( data ) {
console.log(data)
},
error : function( data ) {
console.log('failed');
}
})
})
...and the function it is calling is as follows:
function updateCount() {
$post_id = intval($_POST['post_id']);
echo 'Function has ID as: '. $post_id;
die();
}
This returns the following console output:
AJAX has ID as: 187963
Function has ID as: 0
post_idsounds like a old wp fallback.