簡體   English   中英

AJAX:400 錯誤請求

[英]AJAX: 400 bad request

我一直在尋找幾天的解決方案,但是與使用 AJAX 的 400 錯誤請求相關的所有線程似乎與我的問題無關,或者我只是錯過了它。

我制作了一個頁面,列出了主類別的子類別。 這部分效果很好。 當用戶單擊一個類別時,我需要使用 AJAX 來檢索與用戶剛剛單擊的類別關聯的所有產品。

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>

功能。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');

?>

當我單擊應該觸發 AJAX 的類別之一時,我會收到 400 個錯誤請求。 有任何想法嗎?

您的動作掛鈎處理程序是wp_ajax_get_ajax_posts (和wp_ajax_nopriv_get_ajax_posts ),因此action參數也應該是get_ajax_posts 由於不是,WordPress 以 400 Bad Request 狀態響應您的 AJAX 調用。

這應該有效:

$.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);
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM