【问题标题】:I want to make notification using ajax laravel when I click "Add To Chart" and the notification is Success or failed当我单击“添加到图表”并且通知是成功或失败时,我想使用 ajax laravel 发出通知
【发布时间】:2025-11-30 04:55:01
【问题描述】:

当我点击“添加到购物车”并且通知是成功或失败时,我想使用 ajax laravel 发出通知。

这是我的脚本 ajax

<script>
    $('#formCart').on('click','.add_cart_btn',function(event) {
      $.ajax({
          url: '{{route("add-cart", "glove-botswana-abu-m-f0a9d79f-a575-4221-bead-60ed76afd972")}}',
          type: 'GET',
          dataType: "json",
          data: $('#formCart').serialize(),
          success: function(response){
              $('#overlay').remove();
              if(response.status == "success"){
                  // $.notifyBar({ cssClass: "success", html: "Order successfully added to cart", delay: 500, animationSpeed: "fast" });
                  notification('success', 'Order successfully added to cart');
                  $('.badge').html(response.carts);
                  $('.contentCart').html(response.content);
                  removeCart();
              }else{
                  // $.notifyBar({ cssClass: "error", html: "Order failed added to cart", delay: 500, animationSpeed: "fast" });
                  notification('success', 'Order failed added to cart');
                  removeCart();
              }
          }
      });
  });
</script>

这是我在刀片中的按钮

<button type="button" class=" btn btn-small btn-dark .add_cart_btn">Add to cart</button>

当我运行这个没有任何变化时,按钮没有通知。 我应该怎么做才能在 laravel 中使用 ajax 进行通知

【问题讨论】:

  • 请更具体地说明您面临的问题。目前,我们只知道您的目标 :)
  • 当我运行这个没有任何变化时,按钮没有通知。我应该怎么做才能在 laravel 中使用 ajax 发出通知或警报
  • class=" btn btn-small btn-dark .add_cart_btn" 错误的类名,.add_cart_btn 应该是 add_cart_btn
  • 这个问题是由无法再重现的问题简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。
  • 点击按钮时没有任何变化

标签: javascript php jquery ajax laravel


【解决方案1】:

ajax:

$('.add_cart_btn').on('click',function(event) {
  $.ajax({

按钮:

<button type="button" class="btn btn-small btn-dark add_cart_btn">Add to cart</button>

【讨论】:

  • 没什么可改变的。并且按钮add to cart 在表单&lt;form action="post" id="formCart"&gt;
  • 你试试form action="#"
  • & 尝试使用