代码按照老师的写的,能够在数据库中查询到购物车的变化,但是前端js没有反应


完整代码:
@extends(‘layouts.app’)
@section(‘title’, $product->title)
@section(‘content’)

{ $product->title }}
¥ {
{ $product->price }}
{
{ $product->sold_count }}
{
{ $product->review_count }}
选择
@foreach($product->skus as $sku)
class=”btn btn-default sku-btn”
data-price=”{
{ $sku->price }}”
data-stock=”{
{ $sku->stock }}”
data-toggle=”tooltip”
title=”{
{ $sku->description }}”
data-placement=”bottom”>
{
{ $sku->title }}
@endforeach
{!! $product->description !!}
@endsection
@section(‘scriptsAfterJs’)
$(document).ready(function () {
$(‘[data-toggle=”tooltip”]’).tooltip({trigger: ‘hover’});
$(‘.sku-btn’).click(function () {
$(‘.product-info .price span’).text($(this).data(‘price’));
$(‘.product-info .stock’).text(‘库存:’ + $(this).data(‘stock’) + ‘件’);
});
// 加入购物车按钮点击事件
$(‘.btn-add-to-cart’).click(function () {
// 请求加入购物车接口
axios.post(‘{
{ route(‘cart.add’) }}’, {
sku_id: $(‘label.active input[name=skus]’).val(),
amount: $(‘.cart_amount input’).val(),
})
.then(function () { // 请求成功执行此回调
swal(‘加入购物车成功’, ”, ‘success’);
}, function (error) { // 请求失败执行此回调
if (error.response.status === 401) {
// http 状态码为 401 代表用户未登陆
swal(‘请先登录’, ”, ‘error’);
} else if (error.response.status === 422) {
// http 状态码为 422 代表用户输入校验失败
var html = ‘
_.each(error.response.data.errors, function (errors) {
_.each(errors, function (error) {
})
});
html += ‘
‘;
swal({content: $(html)[0], icon: ‘error’})
} else {
// 其他情况应该是系统挂了
swal(‘系统错误’, ”, ‘error’);
}
})
});
});
@endsection
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230090.html原文链接:https://javaforall.net
