I have an ajax which passes some jQuery variables to PHP function. My function was supposed to be in a class file. But ajax won't work and shows bad request. But if i place php function in my child theme, it works perfectly.
Here is my jQuery AJAX
$.ajax({
type: 'POST',
url: ajaxurl,
data: {
action: 'thwec_get_element_block_data',
block_element_list: block_element_list
},
success:function(data){
alert('success');
console.log(data);
},
error: function(){
alert('error');
}
});
And my PHP function.
public function __construct() {
add_action('wp_ajax_thwec_get_element_block_data',array($this,'get_element_block_data'));
// $this->get_element_block_data();
// $this->init_constants();
}
public function get_element_block_data(){
var_dump("hello");
exit();
}
wp_ajax_nopriv_{$_REQUEST[‘action’]}
hook for non-authenticated Ajax actions for logged-out users.