ID received from post - send via ajax

slick

New Member
#1
I would like to read the post id from html and send it via ajax to the controller. How can I get the ID of $ post->id and transfer it via ajax? Or is there a better solution to save the post seen by the user?
Mã:
@foreach ($posts as $post)
    <div id="post_container_{{$post->id}}" class="row waypoint">
    </div>
@endforeach
This is my ajax code:
Mã:
$('.waypoint').waypoint(function() {

        $.ajax({
            url: '/posts/view',
            type: "post",
            data:
            success: function(request){
                console.log(request);
            },
            error: function(response){
                console.log(response);
            },
            headers:{
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
    }, {
    offset: '100%'
    });
 

Admin

Administrator
Thành viên BQT
#2
Get the id from the focused waypoint.
Mã:
let waypoint_id = this.getAttribute('id'); // something like 'post_container_1'
Get only the string after the _
Mã:
let post_id = waypoint_id.split("_").pop(); // something like '1'
in ajax() function
Mã:
data: {
    post_id: post_id
}
 
Top