本文介绍了Laravel,加入购物车ajax(会话问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,当我尝试异步添加项目到EMPTY CART(空会话)时,它会添加项目,但不会在#counter元素中显示数量。但是,如果我刷新页面,数量会出现在#counter中,从那里我可以异步添加项目没有问题。我很确定问题是,session :: basket变量没有为第一个请求初始化。但是我该如何解决呢?我错过了什么? 我的尝试: 控制器代码: $ newBasket = new 篮($ previousBasket); $ newBasket-> addProduct($ product,$ product-> id); Session :: put(' basket',$ newBasket); $ qtny = Session :: get(' 篮子')? Session :: get(' basket') - >数量: 0 ; return response() - > json([' added' => $ qtny],200); $。ajax({ type: GET, url: / product / add-to-basket / + id, dataType: json, data:{id:id} }) .done( function (数据){ // var ob = JSON.parse(JSON.strigify) $(' #counter')。html(data [' 添加']); swal( 已添加到购物篮!); }); 解决方案 newBasket = new 购物篮( previousBasket); newBasket-> addProduct命令( Hi, guys when I try to add item to EMPTY CART(empty session) asynchronously it adds the item but does not display quantity in the #counter element. But if I refresh the page the quantity appears in #counter and from there I can add items asynchronously no problems. I'm pretty sure the problem is, that session::basket variable isn't initialised for the first request. but how do I resolve it? What am I missing?What I have tried:controller code:$newBasket = new Basket($previousBasket);$newBasket->addProduct($product, $product->id);Session::put('basket', $newBasket);$qtny = Session::get('basket') ? Session::get('basket')->quantity : 0;return response()->json(['added' => $qtny], 200);$.ajax({ type: "GET", url: "/product/add-to-basket/"+id, dataType: "json", data: { id: id } }) .done(function(data) { //var ob = JSON.parse(JSON.strigify) $('#counter').html(data['added']); swal("Added to basket!"); }); 解决方案 newBasket = new Basket(previousBasket);newBasket->addProduct( 这篇关于Laravel,加入购物车ajax(会话问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-15 13:06