I've built a function within a plugin. I'd like to call it with an jQuery ajax function.
The function is defined as follows:
add_action( 'wp_ajax_my_test_ajax', 'my_test_ajax' );
add_action( 'wp_ajax_nopriv_my_test_ajax', 'my_test_ajax' );
function my_test_ajax() {
$resp = array('title' => 'here is the title', 'content' => ' here is the content') ;
wp_send_json($resp) ;
}
I then attempt to call the function with from javascript:
var target = 'http://' + window.location.hostname + '/wp-admin/admin-ajax.php' ;
var data = {
action: ' my_test_ajax'
} ;
var req = jQuery.post( target , data, function(resp) {
console.log(resp.responseText);
}) ;
}
The site responds from admin-ajax.php. The response carries a '200 OK' header. The body of the response is '0'.
The codex tells me that I can expect this response if the value of 'action' in the post body doesn't match the name of a function hooked to the wordpress ajax hooks. But as far as I can tell, I'm good there.
I'm certain that the function is included in my plugin file.
What are the other barest essentials that I need to get this working?
(I don't want to worry about nonces or localizing js at this point. I just want to get a response from the function before I build up further)