After literally hours of trying to hook up a function to handler AJAX requests through WordPress’s API, I am still coming short. I’ve gone back to basic by trying a basic test
<?php
add_action( 'wp_ajax_member_update', 'member_update' );
function member_update ( )
{
echo $_POST['testvariable'];
}
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var data = {
'action': 'member_update',
'testvariable': 1234
};
$.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response); // expected: 1234
});
});
</script>
and even this is returning Status Code 200 and Response 0. What am I missing? I’m perfectly following the documentation as far as I can tell.
If you are setting your
add_action
code in your page, this action won’t be called since you are usingajaxurl
and it usually goes towp-load.php
. You should define your action in your theme’sfunctions.php
code and that will make it be available all over the site (as well as forAJAX
requests).