使用Lazy Load php和AJAX的WooCommerce

时间:2021-08-24 作者:Ferhat

我希望在向下滚动窗口时加载照片,但我在ajax请求中遇到错误,我想知道我看不到的错误是什么?

我得到的错误如下

POST http://localhost/test/wp-admin/admin-ajax.php 400 (Bad Request)

遵循我的代码:jQuery

jQuery(document).ready(function () {
  let limit = 4;
  let start = 0;
  let action = \'inactive\';

  function load_country_data(limit, start) {
    jQuery.ajax({
      url: "http://localhost/test/wp-admin/admin-ajax.php",
      method: "POST",
      data: {limit: limit, start: start, action: \'test\'},
      cache: false,
     
      success: function (data) {
      jQuery(\'#load_data\').append(data);
      if (data === \'\') {
      jQuery(\'#load_data_message\').html("<button type=\'button\'> No Data Found </button>");
         action = \'active\';
       } else {
      jQuery(\'#load_data_message\').html("<button type=\'button\'> Please Wait... </button>")
         action = "inactive";

         }
       }
     });
  }

  if (action === \'inactive\') {
  action = \'active\';
  load_country_data(limit, start);
 }
 jQuery(window).scroll(function () {
  if (jQuery(window).scrollTop() + jQuery(window).height() > jQuery(\'#load_data\').height() && 
   action == \'inactive\') {
   action = \'active\';
   start = start + limit;
   setTimeout(function () {
   load_country_data(limit, start);
     }, 1000);
    }
  });
});

functions.php

add_action(\'wp_ajax_test\', \'test\');
add_action(\'wp_ajax_nopriv_test\', \'test\');

function test(){
    global $wpdb;
    if(isset($_POST["limit"], $_POST["start"])){
        $d = $_POST[\'start\']. ",".$_POST[\'limit\'];
        $list = $wpdb->get_results("Select * From {$wpdb->prefix}load_more ORDER  BY id ASC LIMIT ".$d);
        foreach ($list as $val2) {
            echo \'
        <h3>\'.$val2->header.\'</h3>    
        \';
    }
  }
}
提前感谢您的帮助

enter image description here

实际上,数据值应该为空,但它从不进入if条件,始终返回0

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

这个action 需要加入data:

data: {limit: limit, start: start, action: \'test\'},

相关推荐

在函数中添加可点击函数。php-解析错误

我试图将onclick事件添加到div中。但Wordpress一直说我的函数未定义。未捕获引用错误:未定义thfull函数thfull(){echo";document.getElementById(\'thf\')。style.maxHeight=\'1000px\';}add\\u action(\'wp\\u enqueue\\u scripts\',\'thfull\');html:<;div class=“div class=”;缩略图“;id=“”;thf“;onclick=“”;