duanleiming2014 2018-06-04 02:36
浏览 143
已采纳

在发出Ajax请求Laravel时抛出错误

When I'm doing a request in Ajax, it's throwing me this error.

This is the new error that it's throwing to me:

"message": "Undefined variable: user_id",
    "exception": "ErrorException",
    "file": "/Users/danhergir/Desktop/startup/app/Http/Controllers/ProductReviewController.php",
    "line": 113,
    "trace":

The error is in the line:

$like->user_id = $user_id;

What can I do for solving it?

This is my route.

Web.php

Route::post('/like', [
        'uses' => 'ProductReviewController@postLike',
        'as' => 'like'
    ]);

This is my view file. (This is the part where I have the like/dislike buttons)

Show.blade.php

<div class="container">
        <div class="row">
            <div class="col-md-6" id="user-reviews">
            <h3>Recent comments</h3>
                @forelse($product->reviews as $review)
                <div class="mt-5 border border-dark pl-3 pt-3 pb-3 mb-3 rounded reviewid" data-reviewid="{{ $review->id }}">
                    <div class="title">
                        <h4>{{ $review->headline }}</h4>
                    </div>
                    <div class="user-rating">
                        <star-rating class="pr-3" :star-size="20" :read-only="true" :show-rating="false" :rating="{{ $review->rating }}"></star-rating>
                    </div>
                    <div class="body-text pt-3 pr-5">
                        <p style="text-align:justify"><strong>{{ $review->description }}</strong></p>
                    </div>
                    <div class="body-text pt-3">
                        <h6>
                            <a href="#" class="btn btn-xs btn-warning like">Like</a>
                            <a href="#" class="btn btn-xs btn-danger like">Dislike</a>
                        </h6>
                    </div>
                    <div class="author pt-2">
                        <h6 class="text-muted">{{ $review->user_name }},  {{ date('d-m-Y', strtotime( $review->created_at )) }}</h6>
                    </div>
                </div>
                @empty
                <h6>There are not reviews for this product</h6>
                @endforelse
            </div>
        </div>
    </div>
</div>


@endsection

@section('scripts')
<script src="{{ asset('js/like.js') }}"></script>
<script type="text/javascript">
    var token = '{{ Session::token() }}';
    var urlLike = '{{ route('like') }}';
</script>
@endsection

And finally, this is my JS file.

Like.js

$('.like').on('click', function(event) {
    event.preventDefault();
    reviewId = $(this).closest(".reviewid").attr("data-reviewid");;
    var isLike = event.target.previousElementSibling == null;

    $.ajax({
        method: 'POST',
        url: urlLike,
        data: {isLike: isLike, reviewId: reviewId, _token:token},
    })
});
  • 写回答

2条回答 默认 最新

  • dscw1223 2018-06-04 05:54
    关注

    Use try and catch inside your method like this :

    public function postLike(Request $request) 
    { 
         try{
    
    
            } catch(Exception $e){
                return $e->getMessage();
            } 
    
     }
    

    This will return the error which can be traced by ajax response. And inside storage/logs/laravel.log file you can get the traced error.

    And yes you can use $request['name'] and $request->name both.

    Hope this will help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置