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 类图中关联与聚合的区别
  • ¥15 ENVI高分五号去除云层的方法
  • ¥15 16进制数据如何得到奇偶校验位
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系