问题
Can I use resource via ajax? I have this resource
Route::resource('dashboard', 'DashBoardController');
js file
$.ajax({
type: 'delete',
dataType: 'json',
data: {id:id},
url: " {!! route('dashboard.destroy') !!} ",
success: function (data) {
//
}
});
but I receive,
NotFoundHttpException in RouteCollection.php line 161:
回答1:
A destroy method is using a DELETE request, but it actually uses POST request within an _method as parameter, so your javascript section should looks like this:
$.ajax({
type: 'POST',
dataType: 'json',
data: {
id: id,
_method: 'DELETE'
},
url: "{!! route('dashboard.destroy') !!}",
success: function (data) {
//
}
});
Reference: https://laravel.com/docs/5.2/routing#form-method-spoofing
来源:https://stackoverflow.com/questions/36389669/laravel-resource-destroy-via-ajax