Open up browser dev tools. Click the button to cause the error and go to the network tab. Click on the error respose to see the error received from Laravel.
Ajax post request causes 500 internal server error
In short: 500 internal server error when trying to do a POST request using ajax to a resource controller's method.
Resource CurrencyController:
public function ajaxDeleteImage() {
if ($request->isMethod('ajax')) {
$path = $request->input('imgUrl');
$id = $request->input('id');
$currency = Currency::findOrFail($id);
$currency->cur_icon = '';
$currency->save();
Storage::delete($path);
return 'Deleted image!';
}
return App::abort(404);
}
Here's my routes:
/**
* ajax requests
*/
Route::any('ajax-delete-image-currency', 'CurrencyController@ajaxDeleteImage');
/**
* admin prefix
*/
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function() {
Route::resource('request', 'ReqController');
Route::resource('currency', 'CurrencyController');
});
edit.blade.php - where i created a form with multipart/form-data:
here are some form html code...
@section('custom_scripts')
<script type="text/javascript">
(function($) {
var thisUrl = $('input[name="image-hidden"]').val();
var currencyId = $('input[name=id]').val();
// console.log(thisUrl);
$('.del-image').click(function() {
$.ajax({
method: 'post',
url: '/ajax-delete-image-currency',
data: {imgUrl: thisUrl, id: currencyId},
success: function(data) {
console.log("removed image");
window.location.reload();
},
error: function(e) {
alert('Error' + e);
}
});
});
})(jQuery);
</script>
@endsection
The problem is - i am getting internal server error 500 whenever i press on a button with class .del-image (sending post request with ajax).
Also i tried to delete whole method ajaxDeleteImage from a controller at all. Nothing happened, same 500 error. Seems like post request cannot reach that method in a CurrencyController.
Maybe i have a problem in my routes? Wrong sorting? I read that i have to move certain methods above resource controllers, so it would work correctly, but that it's not my case.
Please advice something.
Try this:
data: {imgUrl: thisUrl, id: currencyId, _token: token}
Please or to participate in this conversation.