David M
David M

Reputation: 29

AJAX: 400 bad request

I have been searching for a couple days now for a solution but all threads related to a 400 bad request using AJAX seem to not be relevant to my issue or perhaps I am just missing it.

I made a page that lists the subcategories of a master category. This part works great. When a user clicks on a category, I need to use AJAX to retrieve all of the products associated with the category that the user just clicked.

page_template.php

<script type="text/javascript">
jQuery(function($) {
    $('.seamBuilder_trigger').click(function() {
        
        var catID = $(this).attr('id');
        
        console.log(catID);
        
        $.ajax({
            type: 'POST',
            url: '<?php echo admin_url('admin-ajax.php');?>',
            dataType: "html", // add data type
            data: {
                action : 'get_products'
            },
            success: function(response) {
                console.log(response);

                $('.posts-area').html(response);
            }
        }); 
        return false;
    });
});
</script>

<section id="content" class="site-content">
    <div id="seam-builder-wrap" class="container">
        <?php
            $masterCat = 39; // Master category 'Seam Builder' ID is 39)
            $taxonomyName = "product_cat";
            $termchildren = get_term_children($masterCat, $taxonomyName);
            
            foreach ($termchildren as $child) {
                $subCat = get_term_by('id', $child, $taxonomyName); //assigns $subCat to the current subcategory in the loop
                $subCat_title = $subCat->name; //gets the name of the subcategory
                $cat_id = $subCat->term_id; //gets the ID of the subcategory on its own
                $thumbnail_id = get_term_meta($child, 'thumbnail_id', true); //gets the thumbnail of the subcategory
                $image = wp_get_attachment_url($thumbnail_id); //gets the URL of the thumbnail
        ?>

        <a id="<?php echo $cat_id ?>" class="seamBuilder_trigger" href="#">
            <img src="<?php echo $image ?>" />
            <span><?php echo $subCat_title ?></span>
        </a>
            <?php } //ends for foreach loop above ?>
    </div>
    
    <div class="posts-area">
        
    </div>
    
</section>

functions.php

<?php

function get_products() {
    
    $cat_id = (isset($_POST['cat'])) ? $_POST['cat'] : '';
    
    echo 'hello there' . $cat_id;
    
    // Query Arguments
    $args = array(
        'cat'               => $cat_id,
        'post_type'         => array('products'),
        'post_status'       => array('publish'),
        'posts_per_page'    => -1,
    );

    // The Query
    $ajaxposts = new WP_Query($args);

    $output = '';

    if ($ajaxposts -> have_posts()) : while ($ajaxposts -> have_posts()) : $ajaxposts -> the_post();
        $output .= 'div class="seamBuilderRow">';
        $output .= $cat_id;
        $output .= '</div>';
        
    endwhile;
    endif;
    wp_reset_postdata();
    wp_die();
}

// Fire AJAX action for both logged in and non-logged in users
add_action('wp_ajax_get_ajax_posts', 'get_products');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_products');

?>

I get 400 bad request as soon as I click on one of my categories that should fire AJAX. Any ideas?

Upvotes: 0

Views: 483

Answers (1)

cabrerahector
cabrerahector

Reputation: 3948

Your action hook handler is wp_ajax_get_ajax_posts (and wp_ajax_nopriv_get_ajax_posts), hence the action parameter should be get_ajax_posts as well. Since it's not, WordPress responds to your AJAX call with a 400 Bad Request status.

This should work:

$.ajax({
    type: 'POST',
    url: '<?php echo admin_url('admin-ajax.php');?>',
    dataType: "html", // add data type
    data: {
        action : 'get_ajax_posts'
    },
    success: function(response) {
        console.log(response);

        $('.posts-area').html(response);
    }
});

Upvotes: 1

Related Questions