WordPress中的Ajax返回400个不良请求,但不确定如何进一步调试



我一直在尝试使用ajax将页面(或其他任何内容(的数据输出到DIV中。数据基于页面上的列表。尽管呼叫AJAX请求的jQuery函数似乎可以正常工作,但我从AJAX调用的控制台上获得了400个不良请求。如何调试此问题,因为我无法从AJAX回调函数

获得任何台。

我有一个WordPress文件,该文件输出产品类别列表和一些jQuery,当单击列出的类别之一时,它们会发射。此jQuery具有AJAX POST函数,可以调用我的重新定位功能。此函数似乎没有任何操作,即使是简单的console.log,Ajax调用的错误显示出错误是400个不良请求。使用WPENGINE密码保护的登台站点,它可能会有所作为。

in functions.php
--------------------

--------------------
in funcitons.php
--------------------
        // for displaying products in div on category page
function enqueue_show_products_scripts () {
        // get the javascript file and enqueue it, then ad it to the     wp_print_scripts action
        $script_location = get_stylesheet_directory_uri() . "/includes/js/show_products.js";
        wp_enqueue_script( 'ajax-script', $script_location, array ('jquery') );
        wp_localize_script( 'ajax-script', 'ajax', array('url' => admin_url( 'admin-ajax.php' )) );
    }
    add_action ('wp_print_scripts', 'enqueue_show_products_scripts');
    // this is the function that the Ajax request is running
    function show_products_callback() {
        // Get the request data	global $wpdb;
    ?>
    <script type="text/javascript">
        console.log ("in show_products_callback");
    </script>
    <?php
        // get_the_data
        //echo(get_term_link($_POST['term_id']));
        //get_term_link($_POST['term_id']);
        //get_term_link(680);
        //print_r($_REQUEST);
        die();
    }
    // and this adds the actions on init to register the function with the Ajax and
    add_action ( 'wp_ajax_nopriv_show_products', 'show_products_callback' );
    add_action ( 'wp_ajax_show_products', 'show_products_callback' );
    in show_product.js
    ----------------------
    jQuery(document).ready( function() {
        jQuery('#categoryListBox a').click ( function () {
        var term_id_clicked = jQuery(this).attr('id');
        alert ("Clicked " + term_id_clicked );
        jQuery.post  ( {
          url: ajax.url,
          action: 'show_products',
          term_id: term_id_clicked,
          success: function ( data ) {
            console.log (data),
            jQuery('#productBox').html(data)
          },
          error: function (errorThrown) {
            console.log ("errorThrown"),
            console.log (errorThrown)
          }
        }
        );  // end jQuery.ajax
    }); // end jQuery click
    });
    jQuery( document ).ajaxError(function( event, data, settings, exception ) {
        alert( "Triggered ajaxError handler." +event+" "+data+" "+settings+" "+exception);
    });
    in show_all_products_by_category.php
    ---------------------------------
    <div id='categoryListBox'>
    <h3>All products by Category</h3>
    ...
          <ul class="categoryUL">
    <?php
            $child_product_cats = get_terms( $child_args );
            foreach ($child_product_cats as $child_product_cat)
            {
                $name = $child_product_cat->name;
                $slug = $child_product_cat->slug;
                $term_id = $child_product_cat->term_id;
    ?>
                <li id='categoryLI-<?= $slug ?>'>
                  <a id='<?= $term_id ?>' href='#'><?=    $child_product_cat->name?></a>
                </li>
    <?php
                // the id is the slug of the subcategory, and the action is performed in the jQuery event
            } // end FOREACH CHILD
    ?>
            </ul>
    ...
    </div>
I keep getting error 400 bad request in the console, but am stumped on how to further debug, as no output from the callback function appears when I echo, alert or console.log.

列表项目

in show_product.js
----------------------
jQuery(document).ready( function() {
    jQuery('#categoryListBox a').click ( function () {
    var term_id_clicked = jQuery(this).attr('id');
    alert ("Clicked " + term_id_clicked );
    jQuery.post  ( {
      url: ajax.url,
      action: 'show_products',
      term_id: term_id_clicked,
      success: function ( data ) {
        console.log (data),
        jQuery('#productBox').html(data)
      },
      error: function (errorThrown) {
        console.log ("errorThrown"),
        console.log (errorThrown)
      }
    }
    );  // end jQuery.ajax
}); // end jQuery click
});
jQuery( document ).ajaxError(function( event, data, settings, exception ) {
    alert( "Triggered ajaxError handler." +event+" "+data+" "+settings+" "+exception);
});

in show_all_products_by_category.php
---------------------------------
<div id='categoryListBox'>
<h3>All products by Category</h3>
...
      <ul class="categoryUL">
<?php
        $child_product_cats = get_terms( $child_args );
        foreach ($child_product_cats as $child_product_cat)
        {
            $name = $child_product_cat->name;
            $slug = $child_product_cat->slug;
            $term_id = $child_product_cat->term_id;
?>
            <li id='categoryLI-<?= $slug ?>'>
              <a id='<?= $term_id ?>' href='#'><?=    $child_product_cat->name?></a>
            </li>
<?php
            // the id is the slug of the subcategory, and the action is performed in the jQuery event
        } // end FOREACH CHILD
?>
        </ul>
...
</div>

我一直在控制台中遇到错误400不良请求,但是我对如何进一步调试感到困惑,因为当我回声,警报或console.log.log。

只是弄清楚了。

我对jQuery.post进行了代码,如下所示: 原始 - 不起作用:

jquery.post({ URL:ajax.url, 动作:'show_products', term_id:term_id_clicked, 成功:功能(数据({

到这种格式,该格式现在以200代码返回:

jQuery.post  ( {
  url: ajax.url,
  data : {
     action : 'show_products',
     term_id : term_id_clicked
  },
  success: function ( data ) {

顺便说一句,与WPEngine联系,即使网站处于"可转让"模式(需要用户名和密码(,这不是问题。

最新更新