Level 74
Show us the full 500 error, You have something wrong in your php code.
1 like
in the console it will print
requisition/15/files:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error
what I am trying to do is uploading a file + change status through a modal form without refreshing and closing the modal
I think I didnot passed my data correctly to the ajax request can anyone solve it for me?
controller
public function storeFile(Request $request , $id) {
if ($request->ajax()) {
$validator = Validator::make($request->all(), [
// $request->validate([
'acc_status' => 'required',
'acc_document' => 'required|mimes:doc,docx,pdf,txt,zip|max:2000',
]);
if ($validator->fails())
{
return response()->json(['errors'=>$validator->errors()]);
}
$requisition = Requisition::find($id);
$requisition->acc_status = $request->get('acc_status');
if($request->hasFile('acc_document'))
{
$requisitionFile = public_path("/{$requisition->acc_document}");
if ($requisition->acc_document!=null)
{
unlink($requisitionFile);
}
$FileName = uniqid() .$request->file('acc_document')->getClientOriginalName();
$path = $request->file('acc_document')->storeAs('uploads', $FileName , 'public');
$requisition->acc_document = '/storage/' . $path;
}
$requisition->save();
return response()->json('success', 'Your file has been uploaded successfully.');
}
}
ajax
<script>
$(document).on('submit', '#FileUploadForm', function (event) {
event.preventDefault();
let href = $(this).attr('action');
var formData = new FormData($('#FileUploadForm')[0]);
// let data = $(this).serialize();
$.ajax({
url: href,
method : 'POST',
data: formData,
processData: false,
contentType: false,
cache: false,
enctype: 'multipart/form-data',
dataType: "json",
headers: {
'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
},
beforeSend: function () {
$('#response').html('<span class="text-primary">Loading response...</span>');
},
success: function (response) {
let data = JSON.stringify(response);
alert(data);
},
error: function (jqXHR, testStatus, error) {
alert("Page " + href + " cannot open. Error:" + error);
},
timeout: 8000
});
});
</script>
form
<form action="{{ route('storefile' , $requisition->id) }}" method="POST"
enctype="multipart/form-data" id="FileUploadForm">
@csrf
<div class="form-group row">
<div class="col-sm-12">
<label for="title"> Account Status: </label>
<select class="form-control" name="acc_status">
<option value="0" {{ $requisition->acc_status == 0 ? 'selected' : '' }}> Inactive
</option>
<option value="1" {{ $requisition->acc_status == 1 ? 'selected' : '' }}> Active
</option>
<label for="" id="accstatus"></label>
</select>
</div>
<div class="col-sm-12 pt-4">
<label for="title"> Account document File: </label>
<div>
@if (!empty($requisition->acc_document))
<label class="badge-success">
{{ $requisition->acc_document }}
</label>
@else
<label class="badge-danger">
Nothing uploaded </label>
@endif
</div>
<input type="file" name="acc_document" class="form-control" id="acc_document" />
<label for="" id="accdocument"></label>
</div>
</div>
<div class="card-footer">
<div class="row">
<div class="col-md-6 text-left">
<div id="response">
<input type="submit" id="submitUpload" value="Upload document"
class="btn btn-primary">
</div>
</div>
</div>
</div>
</form>
route
Route::post('requisition/{id}/files', [RequisitionController::class, 'storeFile'])->name('storefile');
The problem was with my success JSON message in my controller I were able to fix it by changing the error message in to an array.
changing this
return response()->json('success', 'Your file has been uploaded successfully.');
to
return response()->json(['success' => 'Your file has been uploaded successfully!' ]);
Please or to participate in this conversation.