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) {
      var product_id = jQuery(this).attr('href');
            function Ping(product_id) {
                    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) {
            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.

  • 1
    Remove space before wp_ajax from add_action(' wp_ajax_my_action', 'my_action'); add_action(' wp_ajax_nopriv_my_action', 'my_action'); Commented Dec 25, 2019 at 5:03
  • 1
    Thanks, really appreciate it. Commented Dec 25, 2019 at 5:55

1 Answer 1


The problem is that, you have added extra white space before wp_ajax action. remove space from actions ' wp_ajax_my_action' and ' wp_ajax_nopriv_my_action'. because of extra space before ajax request, showing the 400 bad request error. You should use :

add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');
  • 1
    Thanks a lot, brother. Commented Dec 25, 2019 at 5:54
  • Welcome Brother! Commented Dec 25, 2019 at 6:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.