我用laravel 5.2 ajax开发了简单的形式来增加或减少产品数量的价值
这是查看ajax_help.blade.php
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<form name="cart_form">
{{ csrf_field() }}
<input type="hidden" class="item_id" value="1">
<div class="cart_quantity_button">
<button type="button" class="cart_quantity_up" data-increase="1" > + </button>
<input class="cart_quantity_input" type="text" name="quantity" value="1" autocomplete="off" size="2">
<button class="cart_quantity_down" data-increase="0"> - </button>
</div>
</form>
<script>
$('.cart_quantity_up,.cart_quantity_down').on('click', function(e) {
e.preventDefault();
var $this = $(this),
// url = $this.data('route'),
increase = $this.data('increase');
updateQty(increase);
});
function updateQty( increase){
var $qty = $('.cart_quantity_input'),
itemId = $('.item_id').val();
$.ajax({
type: 'POST',
url:'cartajax',
dataType: 'json',
data: {
'_token': $('input[name="_token"]').val(),
'cart_qty': $qty.val(),
'item_id': itemId,
'increase': increase
},
success:function(data){
$qty.val(data.qty);
}
});
}
</script>
</body>
这是控制器功能ajax
public function ajax(Request $request){
if ($request->ajax()) {
$id = $request->item_id;
$rowId = Cart::search(array('id' => $id));
$item = Cart::get($rowId[0]);
if ($request->increase) {
$n_qty = $item->qty + 1;
} else {
$n_qty = $item->qty - 1;
}
Cart::update($rowId[0], $n_qty);
return response()->json(['nqty' => $n_qty]);
}
}
这是路线
Route::get('/ajax',function(){return view('ajax_help');});
Route::Post('/cartajax','front@ajax');
但我得到500内部服务器错误。 这是我的日志控制台 **
**
请有人帮助我