ajax回复正常,但是第二次加载时

时间:2018-07-25 10:21:10

标签: ajax

我正在使用Ajax更新购物车数量。成功更新购物车后,当我加载购物车和当前页面的正文时,我的购物车按钮的切换菜单第一次不起作用。但是当我在加载购物车和车身后第二次更新时,切换菜单起作用了。

我当前的代码:

'remove': function(product_id, flag) {
    // alert(product_id);
    // alert(flag);
    $.ajax({
        url: 'includes/addToCart.php',
        type: 'post',
        data: 'product_id=' + product_id + '&flag='+flag,
        dataType: 'json',
        beforeSend: function() {
            $('#cart > button').button('loading');
        },
        complete: function() {
            $('#cart > button').button('reset');
        },
        success: function(json) {
            // Need to set timeout otherwise it wont update the total
            setTimeout(function () {
                $('#cart > button').html('<span class="lg">My Cart</span><span><i class="fa fa-shopping-basket"></i> ('+json['total']+') items</span>');
            }, 100);
            setTimeout(function () {
                // if(json['key'] == "blank")
                $('#cart > ul').load('includes/loadcart.php');
            }, 100);
             setTimeout(function () {                   
                $('body').load('cart.php');
             }, 100);
        },
    });

在控制台中,执行该错误:

    Synchronous XMLHttpRequest on the main thread is deprecated because of 
    its detrimental effects to the end user’s experience.

我使用了async:true和async:false 同样的问题仍然存在

这是我的html代码,显示值

<div class="btn-group btn-block" id="cart">
              <button class="btn btn-viewcart dropdown-toggle" data-loading-text="Loading..." data-toggle="dropdown" type="button" aria-expanded="false" id="cart_total"><span class="lg">My Cart</span><span><i class="fa fa-shopping-basket"></i> (<?=$_SESSION['totalitem']?>) items</span></button>
              <ul class="dropdown-menu pull-right" ><li>
                  <table id="cart_item" class="table table-striped">
                            <tbody>
                             <?php
             if(empty($_SESSION['cart'])){
            ?>
            <tr>
                <td align="center" style="font-weight: bold; font-size: 14px;" colspan="5">Your Cart Is empty</td>                
            </tr>
            <?php
            }
            else{
            $subtotal = 0;
            foreach($_SESSION['cart'] as $key=>$value){
                $subtotal = $subtotal + ($_SESSION['cart'][$key]['price']*$_SESSION['cart'][$key]['qty']);
            ?>
                              <tr>
                      <td class="text-center">            <a href="#"><img class="img-thumbnail" title="iPhone" alt="iPhone" src="../photo/<?=$_SESSION['cart'][$key]['img']?>" width="57px" height="57px"></a>
                        </td>
                      <td class="text-left"><a href="product.php?id=<?=$_SESSION['cart'][$key]['id']?>"><?=$_SESSION['cart'][$key]['name']?></a>
                                    </td>
                      <td class="text-right">x <?=$_SESSION['cart'][$key]['qty']?></td>
                      <td class="text-right">$<?=$_SESSION['cart'][$key]['price']*$_SESSION['cart'][$key]['qty']?></td>
                      <td class="text-center"><button class="btn btn-danger btn-xs" title="Remove" onclick="cart.remove('<?=$_SESSION['cart'][$key]['id']?>','delete');" type="button"><i class="fa fa-times"></i></button></td>
                    </tr>
                    <?php
                  }
                }
                   $vat= $subtotal * (20/100);
                                  $total = $subtotal + $vat;
                  ?>
                                  </tbody></table>
                </li><li>
                  <?php
                    if(!empty($_SESSION['cart']))
                    {
                    ?>
                  <div>
                    <table class="table table-bordered">
                                <tbody><tr>
                        <td class="text-right"><strong>Sub-Total</strong></td>
                        <td class="text-right">$<?=$subtotal?></td>
                      </tr>
                                <tr>
                        <td class="text-right"><strong>Eco Tax (-2.00)</strong></td>
                        <td class="text-right">$2.00</td>
                      </tr>
                                <tr>
                        <td class="text-right"><strong>VAT (20%)</strong></td>
                        <td class="text-right">$<?=$vat?></td>
                      </tr>
                                <tr>
                        <td class="text-right"><strong>Total</strong></td>
                        <td class="text-right">$<?=$total?></td>
                      </tr>
                              </tbody></table>
                    <p class="text-right"><a href="cart.php"><strong><i class="fa fa-shopping-cart"></i> View Cart</strong></a>&nbsp;&nbsp;&nbsp;<a href="#"><strong><i class="fa fa-share"></i> Checkout</strong></a></p>
                  </div>
                  <?php
                }
                ?>
                </li></ul>
            </div>

0 个答案:

没有答案