如何获得data-id Laravel的价值

时间:2018-06-04 00:48:40

标签: jquery ajax laravel laravel-5

我试图获取正在查看的data-id的值。我试图获得" data-reviewid"。

这是我的观点。

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="comment 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>

这是我的JS文件,我试图获取它,但每次我在reviewId中执行console.log时它都会返回我,因为它未定义。

App.js

$('.like').on('click', function(event) {
    event.preventDefault();
    reviewId = event.target.parentNode.parentNode.dataset['reviewid'];
    var isLike = event.target.previousElementSibling == null;

    $.ajax({
        method: 'POST',
        url: urlLike,
        data: {isLike: isLike, reviewId: reviewId, _token:token},
        success: function( data ) {
        },
        error: function(xhr, status, error) {
           // check status && error
        },
        dataType: 'text'
    })
    console.log(reviewId)
});

现在,我可以获取数据集的数据,但现在的问题是,当我执行POST请求时,它会向我发出下一个错误:

POST http://localhost:8000/like 500 (Internal Server Error)
send @ app.js:12639
ajax @ app.js:12245
(anonymous) @ like.js:6
dispatch @ app.js:8222
elemData.handle @ app.js:8030

我怎样才能看到更好的错误?

1 个答案:

答案 0 :(得分:1)

由于使用closest()data()

,使用jQuery很简单
$('.like').on('click', function(event) {
    event.preventDefault();
    var revId = $(this).closest('.comment').data('reviewid')
   ....