Muhammad Muazzam
Muhammad Muazzam

Reputation: 2800

ajax not calling controller function laravel

I am using following code in javascript function

 $.ajax({
    type: "DELETE",
    url: '/delprofile',
    success:alert("Record deleted."),
        error:  alert("Record not deleted.")
});

and my route and function are as :

public function delprofile (Request $request){  
   DB::table('education')->where('id','=',7)->delete();
   return true;
}

Route::post('/delprofile','ProfileController@delprofile');

The query is not performing any deletion.

Upvotes: 0

Views: 967

Answers (3)

Muhammad Muazzam
Muhammad Muazzam

Reputation: 2800

added CSRF token in ajax code

data: {'_token': $('meta[name="csrf-token"]').attr('content')
            },

Discussed here

Upvotes: 0

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

I think the main issue comes from your JS code, you're missind the anonymos function in the both success & error :

$.ajax({
    type: "DELETE",
    url: '/delprofile',
    success: function(){ alert("Record deleted.") },
    error:  function(){ alert("Record not deleted.") },
});

Or you could also use done/fail instead :

$.ajax({
    type: "DELETE",
    url: '/delprofile',
}).done(function() {
    alert("Record deleted.");
}).fail(function() {
    alert("Record not deleted.");
});

Hope this helps.

Upvotes: 1

Björn Tantau
Björn Tantau

Reputation: 1614

As your AJAX is setting the request-method to DELETE you'll have to do the same with your route.

Route::delete('/delprofile','ProfileController@delprofile');

Upvotes: 4

Related Questions