0

Same function is working If I change type in Ajax as get and route as " Route::post('/delete','CandidateController@remove');". In short get is working fine but post is not. I am getting this error in console

VM2216:1 POST http://localhost/nor_management/public/delete 419 (unknown status) (anonymous) @ VM2216:1 send @ app.js:29 ajax @ app.js:29 (anonymous) @ home:110 dispatch @ app.js:29 g.handle @ app.js:29 VM2216:1 XHR failed loading: POST "http://localhost/nor_management/public/delete".

Ajax call:

 $(document).on('click','#delete',function(){
                    var dataId = $(this).data("id");

                    $.ajax({

                        type:'POST',
                        url:"{!! URL::to('delete') !!}",
                        data:{'id':dataId,},
                        dataType: 'JSON',


                        success:function(data){
                            console.log('success');
                            console.log(data);
                            console.log(data.length);
                           $("#table").load("data");
                        },
                        error:function(){

                        },
                    });
                });
            });

Form:

<div class="container">
    <div class="row">
        <div class="col-md-12"  >
            <div class="panel panel-default" >
                <div class="panel-heading"><a href="add">Add New Candidate</a></div>

                <div class="panel-body">
                        <meta type="hidden" name="csrf-token" content="{{csrf_token()}}">
                    <div class="table-responsive" >

                        @include('data')
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
</form>

Routes:

Route::post('/delete','CandidateController@remove');

Controller:

public function remove(Request $request){
        $id  = $request->id;
        $candidate = new Candidate;
        $candidate->where('id', '=', $id)->delete();
    }
1
  • I'd be looking in the server-side error log Commented Sep 13, 2017 at 6:00

1 Answer 1

1

Try this:

$(document).on('click','#delete',function(){
    var dataId = $(this).data("id");
    var token = $('meta[name="csrf-token"]').attr('content');
    $.ajax({

        type:'POST',
        url:"{!! URL::to('delete') !!}",
        dataType: 'JSON',
        data: {
            "_method": 'POST',
            "_token": token,
            "id": dataId,
        },
        success:function(data){
            console.log('success');
            console.log(data);
            console.log(data.length);
           $("#table").load("data");
        },
        error:function(){

        },
    });
});

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot it worked :), what I have found is that you have to send token and method inside data as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.