Answer the question
In order to leave comments, you need to log in
Where is the bug in Ajax request (Laravel 5.3)?
All HTML
<body>
<table>
<tbody>
<tr>
<td>
User id: {{ $user->id }}
</td>
<td>
<button class="btn btn-danger btn-block delete_single"
data-id="{{ $user->id }}" data-token="{{ csrf_token() }}">
Delete
</button>
</td>
</tr>
</tbody>
</table>
</body>
<script>
jQuery(document).ready(function($) {
$(".delete_single").click(function(){
var id = $(this).data("id");
var token = $(this).data("token");
$.ajax(
{
url: "/users/delete/id="+id,
type: 'DELETE',
data: {
"id": id,
"_method": 'DELETE',
"_token": token,
},
done: function (){
console.log("SUCCESSS!!!");
},
fail: function(){
console.log("ERROR!");
}
});
});
});
</script>
public function delete( Request $user_id ){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}
Route::delete('users/delete/id={user_id}', '[email protected]');
// в консоли
DELETE http://lara53/users/delete/id=14 500 (Internal Server Error)
// на странице по ссылке удаляемого ID
MethodNotAllowedHttpException in RouteCollection.php line 218:
...
at RouteCollection->methodNotAllowed(array('DELETE')) in RouteCollection.php line 205
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
Answer the question
In order to leave comments, you need to log in
public function delete( Request $user_id ){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}
I don't know much about Lara, but I think that's the way it should be.
public function delete(Request $request, $user_id){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}
public function delete($user_id){
$user = User::find($user_id)->delete();
return "SUCCESS!!!";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question