简体   繁体   English

使用 AJAX 将多个项目添加到购物车,只处理一个

[英]Add multiple items to shopping cart with AJAX, only one is processed

I have a shopping cart to which items are added with an AJAX request.我有一个购物车,其中添加了 AJAX 请求的项目。 The request posts to Laravel 5.4.该请求发布到 Laravel 5.4。 It all works fine when I click multiple 'Add To Cart' buttons slowly after each other.当我缓慢地依次单击多个“添加到购物车”按钮时,一切正常。 Then the AJAX requests aren't executed at the same time.然后 AJAX 请求不会同时执行。 But when I click those buttons fast after each other, the AJAX requests are executed at the same time and only one item is added to the cart.但是,当我一个接一个地快速单击这些按钮时,AJAX 请求会同时执行,并且只会将一件商品添加到购物车中。

The jQuery: jQuery:

$('.add-to-cart').on('click', function (event) {
    // Get the product/service ID.
    var item = $(this).attr('data-id');

    $.post('/cart', {
        action: 'addItem',
        data: item
    }, 'json').done(function (response) {
        // Hide .add-to-cart and show .go-to-checkout
    });
});

The PHP: PHP:

public function addItem(Request $request)
{
    // Get item from request.
    $item = $request->get('data');

    // Store item in session.
    $request->session()->push('cart.items', [$item]);

    // Return JSON response.
    return response()->json([]);
}

When I use async: false in my AJAX request, all items are added to the cart, no matter how fast I add them.当我在我的 AJAX 请求中使用async: false时,所有商品都会添加到购物车中,无论我添加它们的速度有多快。 But because that option is deprecated, it doesn't feel right to use it.但是由于该选项已被弃用,因此使用它感觉不对。 I've also looked at alternatives like Promises, but I don't know how to use them in my case.我也看过 Promises 之类的替代方案,但我不知道如何在我的情况下使用它们。

I'm trying to figure out why only one item is being processed when multiple items are being added quickly after each other.我试图弄清楚为什么当多个项目彼此快速添加时只处理一个项目。 Hope you guys can help me out!希望你们能帮帮我!

Maybe its possible to collect items in js-code and then pass them to ajax.也许可以在 js-code 中收集项目,然后将它们传递给 ajax。

The jQuery: jQuery:

$('.add-to-cart').on('click', function (event) {
var items = {COLLECT_ITEMS_FUNCTION};
$.post('/cart', {
    action: 'addItems',
    data: items
}, 'json').done(function (response) {
    // Hide .add-to-cart and show .go-to-checkout
});});

The PHP: PHP:

public function addItems(Request $request) {
$items = $request->get('data');

foreach ($items as $item) {
    // Store item in session.
    $request->session()->push('cart.items', [$item]);
}

// Return JSON response.
return response()->json([]);}

It seems like you can use the laravel-locking-session library to keep Laravel from overwriting your session data.似乎您可以使用laravel-locking-session库来防止 Laravel 覆盖您的会话数据。 I haven't used it, but it claims to fix the exact problem you are experiencing.我没有使用它,但它声称可以解决您遇到的确切问题。

https://github.com/rairlie/laravel-locking-session https://github.com/rairlie/laravel-locking-session

The Laravel version 7+ is supporting Session blocking. Laravel 7+ 版本支持 Session 阻塞。 The block method on the route is limit concurrent requests for a given session路由上的block方法是限制给定会话的并发请求

Route::post('/cart', function () {
    //
})->block($lockSeconds = 10, $waitSeconds = 10)

https://laravel.com/docs/session#session-blocking https://laravel.com/docs/session#session-blocking

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM