代码之家  ›  专栏  ›  技术社区  ›  slickness

从post-send通过ajax接收的ID

  •  0
  • slickness  · 技术社区  · 6 年前

    我想从html读取post id并通过AJAX发送到控制器。我怎样才能拿到邮政ID( $post->id

    @foreach ($posts as $post)
        <div id="post_container_{{$post->id}}" class="row waypoint">
        </div>
    @endforeach
    

    这是我的AJAX代码:

    $('.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%'
    });
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Tharaka Dilshan    6 年前

    从聚焦航路点获取id。

    let waypoint_id = this.getAttribute('id'); // something like 'post_container_1'
    

    _

    let post_id = waypoint_id.split("_").pop(); // something like '1'
    

    在里面 ajax() 功能

    data: {
        post_id: post_id
    }
    
        2
  •  1
  •   Chukwuemeka Inya    6 年前

    data-id 像这样的属性:

    @foreach ($posts as $post)
        <div id="post_container_{{$post->id}}" data-id="{{$post->id}}" class="row waypoint">
        </div>
    @endforeach
    

    attr()

    $('.waypoint').waypoint(function() {
        let post_id = $(this).attr('data-id');   //this specifies the particular post row in focus.
        $.ajax({
            url: '/posts/view',
            type: "post",
            data: {post_id: post_id}
            //and so on. 
        });
    }, {
    offset: '100%'
    });