I'm trying to implement the ajax in wordpress world but now stuck. Function hook suppose to be called when ajax triggered is not happening.
I check the network
- admin-ajax.php is called but response is null implied that getsomething function is not called.
Anyone have any idea?
Follow are my code.
functions.php
function my_resource() {
wp_enqueue_script('my-jquery',get_template_directory_uri().'/jqfunctions.js');
wp_localize_script( 'my-jquery', 'myback',
array('ajax_url' => admin_url( 'admin-ajax.php' )));
}
add_action('wp_enqueue_scripts', 'my_resource');
jqfunctions.js
jQuery(document).ready(function(){
jQuery("#popular").click(function(){
console.log('popular clicked');
jQuery.ajax({
url: myback.ajax_url,
type: 'POST',
dataType: 'json',
data:{
action: 'getsomething'
},
success: function( response ){
console.log("This is response...");
console.log(response);
},
error: function( error ){
console.log('AJAX error callback....');
console.log(error);
}
});
});
});
admin-ajax.php
function getsomething(){
wp_send_json_error('hey');
}
add_action('wp_ajax_nopriv_getsomething', 'getsomething');
add_action('wp_ajax_getsomething', 'getsomething');