2

I want to delete some data from database using ajax.. I've done the following steps to run..But while I was trying to delete it's shows following Error:

Failed to load resource: the server responded with a status of 404 (Not Found)

Here is my route:

Route::post('/delete/{id}',[
    'uses'=>'ItemController@delete',
    'as' => 'delete'
    ]);

Here is the controller:

public function delete($id)
        {
           Item::find($id)->delete();
            return Redirect::back();            
        }   

And here is my view page:

<html>
    <head>
        <title> Item List</title>
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css">
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    </head>

    <body>
            <div class="container">
                <h3> List Of Courses </h3></br>

                <table class="table table-striped table-bordered dataTable" id="example">
                <thead>
                <tr>
                    <td>Serial No</td>
                    <td>Title</td>
                    <td>Description</td>                    
                    <td>Action</td>
                </tr>
                </thead>
                <tbody>
                <?php $i=1; ?>
                @foreach($items as $row)

                    <tr>
                        <td>{{$i}}</td>
                        <td>{{$row->title }}</td>
                        <td>{{$row->description}}</td>                      
                        <td>    
                            <button type="button" onclick="deleteItem({{ $row->id }})" id="Reco" class="btn btn-danger">Delete</button>         
                        </td>
                    </tr>

                <?php $i++; ?>

                @endforeach
                </tbody>
            </table>
            </div>

            <script>
            function deleteItem(id) {
                $.ajaxSetup({
                        headers: {
                      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
             });
          $.ajax({ 
                    type: "POST", 
                    url: '/delete/'+id,               

                    success: function(result) {
                          console.log(result);
                        }
                });          
            }
            </script>
    </body>
</html>

If anyone find the error I've done, hope you'll help me to find it out.

2
  • data might have already deleted? Commented Aug 30, 2016 at 5:38
  • No... noting get deleted Commented Aug 30, 2016 at 5:42

5 Answers 5

3

Generally passing parameter while deleting is not a good idea. since we can delete the data through url e.g /delete/4 while you may want to delete only after clicking the delete button.

 $.ajax({ 
           type: "POST", 
           url: "{{url('/delete)}}",               
           data:{id:id}
           success: function(result) {
           console.log(result);
           }
       });

route:

Route::post('/delete',[
    'uses'=>'ItemController@delete',
    'as' => 'delete'
    ]);

and, the controller

public function delete(Request $request)
{
   $id=$request->id;
   Item::where(['id'=>$id])->delete();
   return Redirect::back();            
}  
Sign up to request clarification or add additional context in comments.

2 Comments

In your case data get deleted but the problem is doesn't redirect to the page means after deleted data shown in page ..after reload it i can't see the data!
Redirect::back() might be redirecting to same page. you can redirect to the intended route: return redirect('/index')
2

Try this code:

function deleteItem(id) {
$.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
             }
            });
            $.ajax({ 
           type: "POST", 
           url:"{{url('delete')}}", 
           data:{id:id},
           success: function(result) {
           location.reload();
           }
       }); 
          } 

Comments

1

try this code

function deleteItem(id) {
var _token = $("input[name='_token']").val();
 $.ajax({
    type: "POST",
    url: '/task/'+id,
    data: '_method=DELETE&_token=' + _token,
    success: function (result) {
        console.log(result);
    }
 });
}

Comments

0

Try this:

$.ajax({ 
    type: "POST", 
    url: url('/delete'),   // The correct way to call ajax function in laravel
    data:{
        id: id
    },     
    success: function(result) {
          console.log(result);
        }
});

1 Comment

I've tried But same error I'm getting ..Internal server Error
0

You can set a method destroy in your controller which laravel automatically uses for deleting. It accepts id.

public function destroy($id){ Item::find($id)->delete(); echo 'Deleted Successfully.'; }

for ajax just send a method delete with your token.

jQuery.ajax({ url:'your-controller-route/id', type: 'post', data: {_method: 'delete', _token :token}, success:function(msg){ // do stuff } });

Comments

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.