dongwen2794 2018-11-02 14:01
浏览 58

阿贾克斯装载laravel购物车

This problem is relatively complicated, atleast to explain.

I am setting up Ajax to work fluently with my Crinsane shoppingcart-plugin for Laravel. My interface enables you to buy an item from the product-list. This is done with Ajax and works fine. When an item is inserted into your cart, a small bar on the bottom pops up with all the contents of the cart. Here there is a non-submit form (just a blank input) where you can type the desired quantity of each product. With me so far? I retrieve the cart contents with another ajax function, which also works just fine. The problem is the blank quantity update input in the bottom bar. It works, but only after I have refreshed the page. When I type a desired quantity into the input field, nothing happens. But when I refresh it, it works just as I want to.

My suspision is that the Cart session or something else needs to be properly loaded by a refresh before any live input Ajax functions can get to work. Although this seems odd considering that is the entire point of ajax.

My Ajax (for the bottom bar input):

<?php
 for($i=0;$i<$cartAmount; $i++){
?>

 var _token = $("input[name='_token']").val();
 function insert_cartBar(qty = '')
  {
   $.ajax({
   url: '<?php echo url('/cart/bar');?>/'+ $('#cart-product-id-<?php echo 
   $i;?>').val(),
   method:'POST',
  data:{
     qty:qty,
    _token:_token,
    rowId: $('#cart-bar-id-<?php echo $i;?>').val(),
    id: $('#cart-product-id-<?php echo $i;?>').val(),

  },
  dataType:'json',
  success:function(data)
  {
    $('#cartResult').text(data.cartCount);
   console.log('eyyy' + data.rowId);
  },
  error: function(data) {
    var errors = data.responseJSON;
    console.log(errors);
  },
});
}
    $(document).on('keyup', '#cart-<?php echo $i?>', function(){
    var qty = $(this).val();
    insert_cartBar(qty);
  });

My Laravel Controller:

       public function cartBarPost(Request $request, $id) {

         $cartContent = Cart::content();
      $product = Product::find($id);

        $rowId = $request->rowId;
        $qty = $request->qty;

      if($qty != '') {

        Cart::update($rowId, $qty);

       }
        $cartCount = Cart::count();


       $data = array(
           'cartCount' => $cartCount,
          'id' => $id,
           'rowId' => $rowId,

            'qty' => $qty,
       );
       echo json_encode($data);
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch