As Salamu Alaikum, I am developing a theme and I have tried every way of getting a successful ajax request and it is still showing me the 400 bad request error. And there is no solution I found going through one after one question in StackOverflow and Wordpress StackExchange.
Here is the ping.js file from where ajax call has been made.
jQuery('area').on('click', function(e) {
e.preventDefault();
var product_id = jQuery(this).attr('href');
Ping(product_id);
});
function Ping(product_id) {
jQuery.ajax({
type: "POST",
url: ajax_object.ajaxurl,
//cache: false,
//contentType: "application/json; charset=utf-8",
//dataType: "html",
data: { 'action':'my_action'},
success: onSuccesPing,
error: onErrorPing,
});
}
function onSuccesPing(data,status) {
alert(data);
}
function onErrorPing(data,status) {
}
And in functions.php I have the following code
function enqueue_scripts() {
wp_register_script('ajax-script', get_template_directory_uri() .'/js/ping.js', array('jquery'), 1.0 );
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
wp_enqueue_script( 'ajax-script'); // jQuery will be included automatically
}
add_action('wp_enqueue_scripts', 'enqueue_scripts');
function my_action() {
echo 'woooo';
?>
<?php die(); }
add_action(' wp_ajax_my_action', 'my_action');
add_action(' wp_ajax_nopriv_my_action', 'my_action');
Even if I remove the whole wp_ajax_{functions} of functions.php, it gives me the same thing. Seems like the request is not being able to hit the wo_ajax_{function}. Please, Help I have spent a whole 8 hours behind this but still no solution. Thanks in Advance.
Post a Comment