个性化阅读
专注于IT技术分析

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

点击下载

我一直试图使用Ajax将页面的数据(或其他任何东西)输出到div中。数据基于页面上的列表。尽管调用Ajax请求的jQuery函数似乎可以正常工作, 但是从Ajax调用在控制台上却收到了400个错误的请求。如何调试它, 因为我无法从Ajax回调函数中获取任何console.log消息

我有一个wordpress文件, 该文件输出产品类别的列表以及单击列出的类别之一时触发的一些jQuery。该jQuery具有Ajax发布函数来调用我的入队和本地化函数。该函数似乎没有任何作用, 即使是简单的console.log, ajax调用的:error也表明该错误是一个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时, 没有出现回调函数的输出。


#1


只是想通了。

我对jQuery.post进行了如下代码更改:原始-无法正常工作:

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

转换为这种格式, 现在返回200代码:

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

顺便说一句, 联系了WPEngine, 即使该站点处于”可转让”模式(需要用户名和密码), 这也不是问题。

赞(1)
未经允许不得转载:srcmini » WordPress中的Ajax返回400错误请求,但不确定如何进一步调试

评论 抢沙发

评论前必须登录!