如何解决jQuery ajax加载更多问题?

时间:2017-10-10 13:22:01

标签: javascript php jquery ajax

我正在使用jQuery ajax在我的应用程序中使用加载更多功能。这是我的ajax代码:

$(document).ready(function() {
     $(document).on('click','.show_more', function(e) {
                e.preventDefault();
                var ID = $(this).attr('id');
                //$('.show_more').hide();
                //$('.loding').show();
                alert(ID)
                $.ajax({
                    type:'POST',
                    url:'ajax_load_more.php',
                    data:{"id" : ID},
                    success:function(html){
                        $('#show_more_main'+ ID).remove();
                        $('.showmorediv').append(html);
                    }
                });
                e.preventDefault();
            });

为了进行测试,我将警报显示为显示ID,它完美运行。但是ajax代码不起作用。没有错误,也没有找到控制台日志。 我仔细检查 ajax_load_more.php 页面,根据提醒的ID完美运行。

这是我的PHP代码

if(isset($_POST["id"]) && !empty($_POST["id"]))
{

    $queryAll = $conn->query("SELECT COUNT(*) as num_rows FROM product WHERE id < ".$_POST['id']." ORDER BY id DESC");
    $row = $queryAll->fetch_array();
    $allRows = $row['num_rows'];
    $showLimit = 3;

    $query2 = $conn->query("SELECT * FROM product WHERE id < ".$_POST['id']." ORDER BY id DESC LIMIT ".$showLimit);
    $rowCount = $query2->num_rows;
    if($rowCount > 0){ 
        while($row_p = $query2->fetch_array()){ 
            $loadmoreid = $row_p["id"]; ?>
            <div class="col-md-4 col-sm-6 col-xs-12 mar-bot showmorediv">
            <!-- single-product-start -->
            <div class="single-product">
                <div class="single-product-img">
                    <a href="product-details/<?=preg_replace('!\s+!', '-',preg_replace("/[^A-Za-z0-9 \s+]/",' ',$row_p[4]));?>-<?=$row_p[0];?>">
                        <img src="<?=$row_p[13];?>" alt="<?=$row_p[3];?>"/>
                    </a>
                    <span class="sale-box">
                        <span class="sale">-<?php echo $offprice=round((($row_p[5]-$row_p[6])*100)/$row_p[5]); ?>%</span>
                    </span>                                            
                </div>
                <div class="single-product-content">
                    <div class="product-title">
                        <h5>
                            <a href="product-details/<?=preg_replace('!\s+!', '-',preg_replace("/[^A-Za-z0-9 \s+]/",' ',$row_p[4]));?>-<?=$row_p[0];?>"><?=$row_p[3];?></a>
                        </h5>
                    </div>                                            
                    <div class="price-box">
                        <span class="price">Rs.<?=$row_p[6];?></span>
                        <span class="old-price">Rs.<?=$row_p[5];?></span>
                    </div>
                    <div class="product-action">
                        <button class="btn btn-default add-cart" data-id="<?=$row_p[0];?>" data-name="<?=$row_p[3];?>" data-summary="<?=$row_p[3];?>" data-price="<?=$row_p[6];?>" data-quantity="1" data-image="<?=$row_p[13];?>">Add to cart</button>        
                    </div>
                </div>
            </div>
            </div>
            <?php   
            } 
            if($allRows > $showLimit)
            { 
            ?>
                <div class="show_more_main" id="show_more_main<?php echo $loadmoreid; ?>">
                    <span id="<?php echo $loadmoreid; ?>" class="show_more btn btn-primary" title="Load more posts btn btn-primary">More</span>
                    <span class="loding" style="display: none;"><span class="loding_txt">Loading…</span></span>
                </div>
        <?php }
        } 
    }
?>

1 个答案:

答案 0 :(得分:0)

使用jquery ajax error function就可以得到未知错误

https://stackoverflow.com/a/8918298/7419418