Wordpress Ajax 过滤器返回 ajax 400(错误网关)

问题描述 投票:0回答:1

我有一个简单的程序尝试在 WordPress 中使用 ajax 过滤帖子。

我已经尝试了一切,但无论我做什么,ajax 都会返回 400m 错误,任何人都可以看到我的代码有什么问题吗? 已经在这三天了

PHP

<?php $categories = get_categories(); ?>
<ul class="cat-list">
  <li><a class="cat-list_item active" href="#!" data-slug="">All projects</a></li>

  <?php foreach($categories as $category) : ?>
   <li>
  <a class="cat-list_item" href="#!" data-slug="<?= $category->slug; ?>" data-type="projecten">
    <?= $category->name; ?>
  </a>
</li>
  <?php endforeach; ?>
</ul>


<?php 
 $projects = new WP_Query([
   'post_type' => 'customer_stories',
   'posts_per_page' => -1,
   'tax_query' => [
      [
         'taxonomy' => 'category',
         'field'    => 'term_id',
         'terms'    => $termIds, // example of $termIds = [4,5]
         'operator' => 'IN'
      ],
   ]
]);
?>

<?php

    
// The Query.
$the_query = new WP_Query( $args );
// The Loop.
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . esc_html( get_the_title() ) . '</li>';
    }
    echo '</ul>';
} else {
    esc_html_e( 'Sorry, no posts matched your criteria.' );
}

// Restore original Post Data.
wp_reset_postdata(); ?>



阿贾克斯


function filter_projects() {
  $postType = $_POST['type'];
  $catSlug = $_POST['category'];

  $ajaxposts = new WP_Query([
    'post_type' => 'customer_stories',
    'posts_per_page' => -1,
    'category_name' => $catSlug,
    'orderby' => 'menu_order', 
    'order' => 'desc',
  ]);
  $response = '';

  if($ajaxposts->have_posts()) {
    while($ajaxposts->have_posts()) : $ajaxposts->the_post();
      $response .= get_template_part( 'src/page-modules/project-list-item.php' );
    endwhile;
  } else {
    $response = 'empty';
  }

  echo $response;
  exit;
}
add_action('wp_ajax_filter_projects', 'filter_projects');
add_action('wp_ajax_nopriv_filter_projects', 'filter_projects');

JavaScript

 (function($){
$.ajax({
  type: 'POST',
  url : wpajax.url,
  dataType: 'html',
  data: {
    action: 'filter_projects',
    category: $(this).data('slug'),
    type: $(this).data('type'),
  },
  success: function(res) {
    $('.project-tiles').html(res);
  }
});

WP_enqueue

<?php
// Theme css & js

function base_scripts_styles() {
    $in_footer = true;
    $ver = 1;

    // Loads JavaScript file with functionality specific.
    wp_enqueue_script( 'base-script', get_template_directory_uri() . '/dist/js/main.js', array( 'jquery' ), $ver, $in_footer );
    wp_enqueue_script( 'theme-script', get_template_directory_uri() . '/theme.js', array( 'jquery' ), $ver, $in_footer );
    wp_enqueue_script( 'ajax', get_template_directory_uri() . '/inc/ajax_script.js', array( 'jquery' ), $ver, $in_footer );

    // Implementation stylesheet.
    wp_enqueue_style( 'base-main', get_template_directory_uri() . '/dist/css/main.css', array(), $ver );

    // Loads our main stylesheet.
    wp_enqueue_style( 'base-style', get_stylesheet_uri(), array(), $ver );

    wp_localize_script(
        'ajax',
        'wpajax',
        'base-script',
        'themeAjax',
        array(
            'url' => admin_url( 'admin-ajax.php'),
            'nonce' => wp_create_nonce( 'theme_ajax_nonce' ),
            'searchButton' => esc_attr( __( 'Search', 'base' ), ),
        )
    );

    wp_register_script( 'lottie-player', 'https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js', array(), $ver );
}
add_action( 'wp_enqueue_scripts', 'base_scripts_styles' );

function theme_add_admin_styles() {
    wp_enqueue_style( 'admin_css', get_template_directory_uri() . '/admin-style.css', false, '' );
}
add_action( 'admin_enqueue_scripts', 'theme_add_admin_styles' );

也得到这个js错误

jquery-3.6.0.min.js:2 Uncaught Error: Syntax error, unrecognized expression: #!
at se.error (jquery-3.6.0.min.js:2:13639)
at se.tokenize (jquery-3.6.0.min.js:2:21650)
at se.select (jquery-3.6.0.min.js:2:22477)
at Function.se [as find] (jquery-3.6.0.min.js:2:7116)
at S.fn.init.find (jquery-3.6.0.min.js:2:25047)
at new S.fn.init (jquery-3.6.0.min.js:2:25536)
at S (jquery-3.6.0.min.js:2:1051)
at l.t [as getAnchorTarget] (main.js?ver=1:3:153334)
at HTMLAnchorElement.<anonymous> (main.js?ver=1:3:153137)
at jquery-3.6.0.min.js:2:24485
jquery ajax wordpress categories enqueue
1个回答
0
投票

如错误所示,“#!”不是 jQuery 的有效表达式。如果需要,请将其替换为实际链接,否则应将其删除。如果你确实需要使用 '#' 作为 jQuery 选择器,你可以像这样 '\#' 来转义它。

© www.soinside.com 2019 - 2024. All rights reserved.