I'm using Laravel 5.2 with ajax but I have internal server error 500 when I click on the link for example.
Here my code . . .
Blade file:
<a class="delete" href="#" data-userid="{{ $user->id }}">X</a>
Js file:
var userId = 0;
$('.delete').on('click', function(event){
event.preventDefault();
userId = event.target.dataset['userid'];
$.ajax({
method:'POST',
url: 'users/delete',
data: {userId: userId, _token: token}
}).done(function() {
console.log('done');
});
console.log(userId);
});
Route file:
Route::post('users/delete', 'HomeController@delete');
HomeController file:
public function delete(Request $request)
{
$user = POST::find($request['userId']);
$user->delete();
return response();
}
Js file works good out of laravel . . . Of course I have database with users table, and connection with database. I'm working locally and using windows 10.
This is error screenshot:
Can you please try using request Method as DELETE rather than a POST ?
It may fix your error. Here. Below check example for delete.
http://www.sitepoint.com/crud-create-read-update-delete-laravel-app/
[2016-04-30 21:35:42] local.ERROR: exception 'Symfony\\Component\\Debug\\Exception\\FatalErrorException' with message 'Class 'App\\Http\\Controllers\\POST' not found' in C:\\project-user\\app\\Http\\Controllers\\HomeController.php:47
looks like you didn't imported POST with use
keyword or you used wrong class (not POST
)
use this (check if POST is the right name for ur model in App folder)
$user = \POST::findOrfail($request->input('userId');
$user->delete();
return response()->json('Success');
in js
success: function(response){
console.log(response);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.